【问题标题】:Select 1 Updates Select 2, but Select 2 doesn't fire选择 1 更新选择 2,但选择 2 不会触发
【发布时间】:2012-03-12 07:59:29
【问题描述】:

我正在尝试让 Select 1 更新 Select 2,然后让 Select 2 触发。

选择 2 只在直接控制时触发.. 我被难住了!!

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Select Firing</title> 
</head>
<body>
<p>Select 1 updates Select 2, but select 2's alert does not fire</p>
<select id="select1">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
</select>
<p>Select 2 only fires alert when controlled directly</p>
<div>
<select id="select2" onchange="alert(this.value)">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
</select>
</div>
</body>
<script src="js/jquery-1.6.4.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$(function() { //run on document.ready
  $("#select1").change(function() { //this occurs when select 1 changes
    $("#select2").val($(this).val());
  });
});
});
</script>
</html>

谢谢!

【问题讨论】:

    标签: javascript jquery select onchange


    【解决方案1】:
    $("#select1").change(function() {
    
        $("#select2").val($(this).val()).trigger('change');
        //or
        $("#select2").val($(this).val()).change();
    
    });
    

    这是你需要的吗?还是我错过了什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多