2012-07-19 34 views
19

Tôi đang cố tạo nút có thể chọn tùy chọn tiếp theo.Chọn tùy chọn tiếp theo với jQuery

Vì vậy, tôi có một lựa chọn (id = selectionChamp) với một số tùy chọn, một đầu vào tiếp theo (id = fieldNext), và tôi cố gắng để làm điều đó:

$('#fieldNext').click(function() { 
    $('#selectionChamp option:selected', 'select').removeAttr('selected') 
      .next('option').attr('selected', 'selected'); 

    alert($('#selectionChamp option:selected').val());  
}); 

Nhưng tôi không thể chọn tùy chọn tiếp theo .. Cảm ơn !

+0

Bạn đang tải DOM khi tải trang? – Dev

+0

Ý của bạn là gì? –

+0

có nghĩa là bạn đang sử dụng mã đó trong chức năng sẵn sàng? – Dev

Trả lời

22
$('#fieldNext').click(function() { 
    $('#selectionChamp option:selected').next().attr('selected', 'selected'); 

    alert($('#selectionChamp').val());  
}); 
+1

Bị hỏng trong FF 32.0.3 – njahnke

+4

Cần cũng bỏ chọn tùy chọn hiện tại, ví dụ: '$ ('# selectionChamp option: selected'). attr ('đã chọn', sai);' – anthonygore

+0

vâng ... nhưng chỉ khi nhiều thuộc tính được đặt trên thẻ đã chọn. Tuy nhiên, bạn có thể tự do cập nhật câu trả lời nếu muốn đến .... – bugwheels94

0
$('#fieldNext').click(function() { 
$('#selectionChamp option:selected').removeAttr('selected') 
     .next('option').attr('selected', 'selected'); 

alert($('#selectionChamp option:selected').val());  
}); 
12
$("#fieldNext").click(function() { 
    $("#selectionChamp > option:selected") 
     .prop("selected", false) 
     .next() 
     .prop("selected", true); 
});​ 

DEMO:http://jsfiddle.net/w9kcd/1/

0

Hãy thử điều này:

$(document).ready(function(){ 
     $("#fieldNext").on("click",function(){ 
      $optionSelected = $("#selectionChamp > option:selected"); 
      $optionSelected.removeAttr("selected"); 
      $optionSelected.next("option").attr("selected","selected");  
     }); 
    }); 
2
$(function(){ 
    $('#button').on('click', function(){ 
    var selected_element = $('#selectionChamp option:selected'); 
    selected_element.removeAttr('selected'); 
    selected_element.next().attr('selected', 'selected'); 

    $('#selectionChamp').val(selected_element.next().val()); 

    }); 
}); 

http://jsbin.com/ejunoz/2/edit

8

Khá đơn giản mà không cần jQuery. Điều này một ý chí vòng xung quanh để lựa chọn đầu tiên khi người cuối cùng đạt được:

function nextOpt() { 
    var sel = document.getElementById('selectionChamp'); 
    var i = sel.selectedIndex; 
    sel.options[++i%sel.options.length].selected = true; 
} 

window.onload = function() { 
    document.getElementById('fieldNext').onclick = nextOpt; 
} 

Một số đánh dấu kiểm tra:

<button id="fieldNext">Select next</button> 
<select id="selectionChamp"> 
<option>0 
<option>1 
<option>2 
</select> 
+0

+1. Bạn đã đăng một số giải pháp JS vanilla tốt! Giúp đỡ rất nhiều! – A1rPun

0

Các giải pháp khác không có tác dụng trong trường hợp có yếu tố optiongroup ngoài tùy chọn các yếu tố. Trong trường hợp đó, điều này dường như làm việc:

var options = $("#selectionChamp option"); 
var i = options.index(options.filter(":selected")); 
if (i >= 0 && i < options.length - 1) { 
    options.eq(i+1).prop("selected", true); 
} 

(Bạn có thể nghĩ rằng biểu thức cho i cũng có thể được viết như options.index(":selected"), nhưng điều đó không phải lúc nào cũng làm việc tôi không biết tại sao, một lời giải thích sẽ. được chào đón.)

Các vấn đề liên quan