如何使用jQuery在选择框上设置第一个选项? [英] How to set the first option on a select box using jQuery?
本文介绍了如何使用jQuery在选择框上设置第一个选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个HTML 选择
框。当我在另一个选项中做出选择时,我需要重置一个选择
框。
I have two HTML select
boxes. I need to reset one select
box when I make a selection in another.
<select id="name" >
<option value="">select all</option>
<option value="1">Text 1</option>
<option value="2">Text 2</option>
<option value="3">Text 3</option>
</select>
<select id="name2" >
<option value="">select all</option>
<option value="1">Text 1</option>
<option value="2">Text 2</option>
<option value="3">Text 3</option>
</select>
当我选择第一个的选项时选择
(即 id =name
),我需要将第二个选择
重置为全选
;同样,当我选择第二个的选项选择
(即 id =name2
)时,我需要重置第一个选择
到选择所有
。
When I select an option of the first select
(i.e. id="name"
), I need to reset the second select
to select all
; similarly, when I select an option of the second select
(i.e. id="name2"
), I need to reset the first select
to select all
.
怎么能我这样做了吗?
推荐答案
这样的事情可以解决这个问题: https://jsfiddle.net/TmJCE/898/
Something like this should do the trick: https://jsfiddle.net/TmJCE/898/
$('#name2').change(function(){
$('#name').prop('selectedIndex',0);
});
$('#name').change(function(){
$('#name2').prop('selectedIndex',0);
});
这篇关于如何使用jQuery在选择框上设置第一个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文