jQuery change()和bind("change")不起作用

Zee*_*han 2 html javascript ajax jquery dom

这个问题已经被问到了,但我最基本的问题仍然存在.除了select标签并试图通过jquery捕获更改事件,我没有向我的html添加任何内容.这是我的代码:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script>
  $('#target').bind("change", function{
    alert('Changed');
  });
</script>
</head>
<body>
<form>
  <select id="target">
    <option value="option1" selected="selected">Option 1</option>
    <option value="option2">Option 2</option>
  </select>
</form>
</body>
Run Code Online (Sandbox Code Playgroud)

甚至change()函数也不起作用.

<script>
  $('#target').change(function() {
    alert('Changed');
  });
</script>
Run Code Online (Sandbox Code Playgroud)

请帮我弄清楚我做错了什么.在Stackoverflow上的一个类似问题中,我得到了http://jsfiddle.net/78x9Q/4/作为答案.但即使是该代码的精确复制粘贴也不适用于我.

PS:jQuery正在加载,因为我测试它并发现使用它:

<script>
$(function() {
  alert('Changed');
});
</script>
Run Code Online (Sandbox Code Playgroud)

Dan*_*sky 7

您的代码不起作用,因为您尝试使用dom时尚未构建它.用这个:

$(document).ready(function () {
  // your code
});
Run Code Online (Sandbox Code Playgroud)

然后你的代码将在dom已经构建时运行.