更改选择值,另一选择则更改 [英] change selects value onchange of another select

查看:80
本文介绍了更改选择值,另一选择则更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我开始学习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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆