Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

javascript - 如何使用jQuery在选择框上设置第一个选项?(How to set the first option on a select box using jQuery?)

I have two HTML select boxes.(我有两个HTML select框。)

I need to reset one select box when I make a selection in another.(当我在另一个选区中进行select时,我需要重置一个select框。)
<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>

When I select an option of the first select (ie id="name" ), I need to reset the second select to select all ;(当我选择第一选项select (即, id="name" ),I需要重置所述第二selectselect all ;)

similarly, when I select an option of the second select (ie id="name2" ), I need to reset the first select to select all .(类似地,当我选择第二的一个选项select (即, id="name2" ),我需要复位第一selectselect all 。)

How can I do that?(我怎样才能做到这一点?)

  ask by Anish translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something like this should do the trick: https://jsfiddle.net/TmJCE/898/(像这样的东西应该做的伎俩: https//jsfiddle.net/TmJCE/898/)

$('#name2').change(function(){
    $('#name').prop('selectedIndex',0);
});


$('#name').change(function(){
    $('#name2').prop('selectedIndex',0);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...