【问题标题】:Event not firing when OnChange of dropdownlist called调用下拉列表的 OnChange 时事件未触发
【发布时间】:2012-02-20 19:49:58
【问题描述】:

在我的下拉列表中,我对其进行了设置,因此 onchange 应该调用 checkval 并传入元素的 id。我刚开始使用非常基本的登录,但甚至无法显示警报。我做错了什么?

<!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>Untitled Document</title>
<script>
function checkval(id){
    if($('#' + id).val == "Other")
    {
        alert("You Selected Other");
        //other logic will go here
    }
}
</script>
</head>

<body>
<select name="items1[]" id="items1[]" onchange="checkval(id);">
    <option selected value="">Select One</option>
    <option value="one">One</option>
    <option value="Other">Other</option>
  </select>

<select name="items2[]" id="items2[]" onchange="checkval(id);">
    <option selected value="">Select One</option>
    <option value="one">One</option>
    <option value="Other">Other</option>
  </select>
</body>
</html>

【问题讨论】:

    标签: jquery html drop-down-menu


    【解决方案1】:

    编辑: id items1[] 似乎失败了。所以更改了您的标记位.. 在这里修复了jsFiddle

    查看What characters are allowed in DOM IDs?的链接

    将您的标记更改为onchange="checkval(this.id);"

    脚本为

    function checkval(id){
        if($('#' + id).val() == "Other")
        {
            alert("You Selected Other");
            //other logic will go here
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      <select name="items1[]" id="items1[]" onchange="checkval(this.id);">
      

      或者更好的是,不显眼地连接你的处理程序,

      $(document).ready(function(){
          $(".items").change(function(){ 
              checkval(this.id);
          });
      });
      

      并删除 onchange 属性并添加一个类:

      <select name="items1" id="items1" class="items">
          <option selected value="">Select One</option>
          <option value="one">One</option>
          <option value="Other">Other</option>
        </select>
      
      <select name="items2" id="items2" class="items">
          <option selected value="">Select One</option>
          <option value="one">One</option>
          <option value="Other">Other</option>
        </select>
      

      【讨论】:

      • 正如其他人所指出的,不要忘记checkval().val() 的括号。
      【解决方案3】:

      val 是一个函数,所以将它用作一个可以工作的函数val()

      或者,您可以将所选值本身传递给chectVal 方法。

      更改您的标记。

      <select name="items1[]" id="items1[]" onchange="checkval(this.value);">
      <select name="items2[]" id="items2[]" onchange="checkval(this.value);">
      

      JS

      function checkval(value){
          if(value == "Other")
          {
              alert("You Selected Other");
              //other logic will go here
          }
      }
      

      【讨论】:

        【解决方案4】:

        你应该像这样用括号调用 val:

        function checkval(id){
            if($('#' + id).val() == "Other")
            {
                alert("You Selected Other");
                //other logic will go here
            }
        }
        

        正如 SKS 指定的,您在 onchange 事件中也有错误,应该是 this.id。但是,您应该将代码更改为更简单一些。

        变化:

        <select name="items1[]" id="items1[]" onchange="checkval(this);">
        

        还有你的功能:

        function checkval(el){
            if($(el).val() == "Other")
            {
                alert("You Selected Other");
                //other logic will go here
            }
        }
        

        另外,您是否在控制台中遇到任何错误?

        【讨论】:

          【解决方案5】:

          $('#' + id).val 是一个函数。
          函数永远不等于"Other"

          您可能想调用使用括号的函数。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-03-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-29
            相关资源
            最近更新 更多