jquery: change the value of `select1` onchange of `select2` IF $('#select2').val() < $('#select1').val()
i've asked the first question about selects here, i thought, that when i resie开发者_JS百科ve an answer, i would solve and the second part of my problem, but there was a short way to solve first problem, so i have to ask a question again, about second part:
i have two selects
<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>
and i need to change the value of select1
onchange of select2
,
if $('#select2').val() < $('#select1').val();
i try to write the following function
$("#select2").bind('change',function() {
if($('#select2').val() < $('#select1').val())
{
$("#select1").val($(this).val());
}
});
but it doesn't eork, becouse on the moment of change, when i call the function, it doesn't change it's value yet. so, what can i do?
Thakns
try change
$("#select1").val($(this).val());
to
$("#select1 :selected").val($(this).val());
( I assume you want to change the value of the currently selected element in select1 based on what you selected in select2 )
it works for me. Note that you are binding to the select2 change event not the select1 change event. So when you change select 2, select 1 should change if it has a higher value.
精彩评论