简体   繁体   English

无法触发选择列表上的更改事件

[英]unable to fire change event on select list

html: HTML:

<select id="v_name" align="top">
  <option value="server1">server1</option>
  <option value="server2">server2</option>

    </select>

javascript: JavaScript的:

<script>

  $("#v_name").change(function() {
    var selected = $(this).val(); //value of selected attribute
 alert(selected);
  });
</script>

When I change the selected item on the select, alert is not firing. 当我更改选择项上的所选项目时,警报不会触发。 Any ideas 有任何想法吗

The code is OK you just have to make sure the element exists in the DOM before the script executes. 代码是可以的,您只需要确保脚本执行前该元素已存在于DOM中即可。

Put it inside document ready event: 将其放入文档就绪事件:

$(document).ready(function(){

    $("#v_name").change(function() {
        var selected = $(this).val(); //value of selected attribute
        alert(selected);
    });

});

Working sample here: http://jsfiddle.net/carloscalla/y2jppe6m/ 此处的工作示例: http : //jsfiddle.net/carloscalla/y2jppe6m/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM