javascript onchange 事件未在 <select> 元素上触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8390816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:17:59  来源:igfitidea点击:

onchange event not firing on <select> element

javascriptjavascript-events

提问by Ramesh Kotha

I am trying to call show()when the select dropdown changes, but the event is not firing.

我试图show()在选择下拉列表更改时调用,但事件未触发。

<script type="text/javascript"> 
    function show() { 
        alert(document.getElementById("start").value); 
    } 
</script>

<div class="side-by-side clearfix" style="margin-bottom:14px;">
    <select  data-placeholder="Select Loacation" class="chzn-select" tabindex="2" name="source" id="start" onchange="show()">
      <option value="">Select Loacation</option>
      <c:forEach items="${listOfRoutes}" var = "route">
      <option value="${route.source }" >${route.source }</option>
      </c:forEach>
    </select>
</div>

回答by Jeffrey Sweeney

Your html syntax is fine, so there could be an error with the show function. Check your console to make sure.

您的 html 语法很好,因此 show 函数可能会出错。检查您的控制台以确保。

Edit: The event is fired on my end... hmm...

编辑:该事件是在我结束时触发的......嗯......

回答by Morten Anderson

Looks fine. I think your getElementById fails to fetch what you want.

看起来不错。我认为您的 getElementById 无法获取您想要的内容。

Here a small working example of what you want - but as stated. Your syntax looks fine.

这是您想要的一个小工作示例 - 但如上所述。你的语法看起来不错。

<script type="text/javascript">        
  function hello() 
  {
    alert("hello");
  }
</script>

<select id="Select1" onchange="hello()">
  <option>1</option>
  <option>2</option>
</select>

回答by Arun Chudmunge

Try

尝试

<script type="text/javascript"> 
   $("#start").change( function() { 
        alert($("#start").val()); 
    }); 
</script>

<div class="side-by-side clearfix" style="margin-bottom:14px;">
    <select  data-placeholder="Select Loacation" class="chzn-select" tabindex="2" name="source" id="start">
      <option value="">Select Loacation</option>
      <c:forEach items="${listOfRoutes}" var = "route">
      <option value="${route.source }" >${route.source }</option>
      </c:forEach>
    </select>
</div>