jQuery使用Next设置所选选项 [英] jQuery Set Selected Option Using Next

查看:101
本文介绍了jQuery使用Next设置所选选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用jQuery将已选择项的下一个"项设置为已选择".

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

例如,如果我有:

<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>

我们可以看到选择了数字2",并且我想使用jQuery将数字3"设置为选中状态,并从数字2"中删除选中的属性".我假设我需要使用 next 选择器,但我不确定如何实现.

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.

推荐答案

$('option:selected', 'select').removeAttr('selected').next('option').attr('selected', 'selected');

在此处查看工作代码 http://jsbin.com/ipewe/edit

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

这篇关于jQuery使用Next设置所选选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆