jQuery Set Selected Option Using Next

2019-03-09 11:01发布

How can I, using jQuery, set the "next" item of an already selected item as "selected."

For example, if I have:

<select>
<option value="1" >Number 1</option>
<option value="2" selected="selected">Number 2</option>
<option value="3" >Number 3</option>
<option value="4" >Number 4</option>
</select>

We can see that "Number 2" is selected, and using jQuery, I'd like to set "Number 3" as selected, and remove the selected "attribute" from "Number 2". I'm assuming I need to use the next selector, but I'm not quite sure how to implement.

11条回答
2楼-- · 2019-03-09 11:42

Find the row, then

var row = $('#yourTable');

the value you want to select

var theValue = "5";
row.find("select:eq(2)").find("option[value="+theValue+']').attr('selected','selected');
查看更多
趁早两清
3楼-- · 2019-03-09 11:44
  $("select").prop("selectedIndex",$("select").prop("selectedIndex")+1)
查看更多
Explosion°爆炸
4楼-- · 2019-03-09 11:46

From version 1.6.1 on, it's advisable to use the method prop for boolean attributes/properties such as selected, readonly, enabled,...

var theValue = "whatever";
$("#selectID").val( theValue ).prop('selected',true);

For more info, please refer to to http://blog.jquery.com/2011/05/12/jquery-1-6-1-released/

查看更多
做个烂人
5楼-- · 2019-03-09 11:51
$('#next').click( function(){
    $('#colored_background option:selected').next('option').attr('selected', 'selected');
    changeBackgroundColor();
});

Working at What is my favorite color?. Click on the arrows.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-03-09 11:56
$('option:selected', 'select').removeAttr('selected').next('option').attr('selected', 'selected');

Check out working code here http://jsbin.com/ipewe/edit

查看更多
登录 后发表回答