使用Next设置选定的选项
16lz
2021-01-22
How can I, using jQuery, set the "next" item of an already selected item as "selected."
如何使用jQuery设置已选择项的“下一个”项作为“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.
我们可以看到“2号”被选中,使用jQuery,我想将“3号”设置为选中,并从“2号”中移除选中的“属性”。我假设我需要使用下一个选择器,但是我不确定如何实现。
11 个解决方案
#1
60
$('option:selected', 'select').removeAttr('selected').next('option').attr('selected', 'selected');
Check out working code here http://jsbin.com/ipewe/edit
查看这里的工作代码http://jsbin.com/ipewe/edit
更多相关文章
- jsf中获取属性值的普通获取和jquery获取区别
- bootstrap-select js jQuery控制select属性变化
- JQUERY组装对象并调用自身函数改变自己的属性
- 使用表单字段值定制CSS属性。
- js获取html下拉框中选中值的自定义属性值
- 关于INPUT的autocomplete="off" 属性(浏览器表单默认的记忆功能)
- HTML5新增标签与属性
- CSS定位属性之间的相互作用
- HTML:关于a标签的target属性