Jquery只设置一个选定的选项
我使用的是 html select,我只需要从中获取一个选定的值,而在使用 firebug 进行调试时,我看到选择了 2 个选项。
如何使用 jquery 使其互斥?请帮助
<select id="test" title="select table" class="selectChangeClass">
<option value="None">None</option>
<option value="Equal">Equal</option>
<option value="NotEqual">NotEqual</option>
<option value="LessThan">LessThan</option>
<option value="GreaterThan">GreaterThan</option>
<option value="LessthanOrEqual">LessthanOrEqual</option>
<option value="GreaterthanOrEqual">GreaterthanOrEqual</option>
</select>
脚本
$('.selectChangeClass').live('change', function(event) { varchangedText = $(this).val(); var controlId = this.getAttribute('id'); var indexSelected = this.selectedIndex; // this.selectedIndex = indexSelected; $('#test' option[value="' +changedText + '"]').attr('selected', 'selected'); var opt = $('#' + controlId + ' option[value="' +changedText + '"]'); var html = $("
").append(opt.clone()).html(); html = html.replace(/\>/, ' selected="selected">'); opt.replaceWith(html); }); 脚本
I'm using an html select from which i need to get only one selected value, while debugging using firebug i'd seen 2 options are selected.
How can make this mutually exclusive using a jquery? Please help
<select id="test" title="select table" class="selectChangeClass">
<option value="None">None</option>
<option value="Equal">Equal</option>
<option value="NotEqual">NotEqual</option>
<option value="LessThan">LessThan</option>
<option value="GreaterThan">GreaterThan</option>
<option value="LessthanOrEqual">LessthanOrEqual</option>
<option value="GreaterthanOrEqual">GreaterthanOrEqual</option>
</select>
Script
$('.selectChangeClass').live('change', function(event) { var changedText = $(this).val(); var controlId = this.getAttribute('id'); var indexSelected = this.selectedIndex; // this.selectedIndex = indexSelected; $('#test' option[value="' + changedText + '"]').attr('selected', 'selected'); var opt = $('#' + controlId + ' option[value="' + changedText + '"]'); var html = $("<div>").append(opt.clone()).html(); html = html.replace(/\>/, ' selected="selected">'); opt.replaceWith(html); });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
编辑
DEMO
也可以看到如何更改所选选项 http://jsfiddle.net/9ftwG/1/
和此http://jsfiddle.net/9ftwG/4/ 查看如何克隆和更改所选选项
try
EDIT
DEMO
also see this to how to change the selected option http://jsfiddle.net/9ftwG/1/
and this http://jsfiddle.net/9ftwG/4/ to see how to clone and change the selected option
我不完全确定我理解这个问题。您在什么时候选择了两个选项?选择上是否设置了多个?
这是一个 JSFiddle 在选择更改时更新 div:http://jsfiddle.net/aMs8G/。
I am not entirely sure i understand the issue. At what point are you getting two options selected? Is Multiple set on the select?
Here is a JSFiddle updating a div on change of a select: http://jsfiddle.net/aMs8G/.