更改选择值,另一选择则更改 [英] change selects value onchange of another select
问题描述
几天前,我开始学习jquery,我非常喜欢它.但是现在我有一个问题,不能单独解决.
i start learning jquery few days ago, and i like it very much. but now i have a problem, that can't solve alone.
我有两个选择
<select id="select1">
<option value="1">1day</option>
<option value="2">2day</option>
<option value="3">3day</option>
</select>
<select id="select2">
<option value="1">1day</option>
<option value="2">2day</option>
<option value="3">3day</option>
</select>
当#select1
更改时,我需要将#select2
设置为与#select1
相同的值
i need to set #select2
the same value with #select1
, when #select1
changes
i've red some questions about select tag here, but i need to set "selected" attribute to that option, which have the same value. how can i do it?
谢谢
推荐答案
要设置值,请使用 .val()
这样:
To set the value use .val()
like this:
$(function() { //run on document.ready
$("#select1").change(function() { //this occurs when select 1 changes
$("#select2").val($(this).val());
});
});
这会将<select id="select2">
设置为与另一个相同的值. .val(vale)
选择在<select>
中具有相应值的<option>
,并且也取消了选择上一个选择.您可以使用不带参数的 .val()
来获取当前选择的<option>
的值.
This would set <select id="select2">
to the same value as the other one has. .val(vale)
selects the <option>
that has the corresponding value in the <select>
, also de-selecting the previous selection. You use .val()
without parameters to get the currently selected <option>
's value.
这篇关于更改选择值,另一选择则更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!