最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery change() and bind("change") do not work - Stack Overflow

programmeradmin2浏览0评论

This question has already been asked but I am stuck with the most basic level of it. I haven't added anything to my html except a select tag and trying to catch the change event by jquery. Here is my code:

<head>
<script src=".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>

Even the change() function does not work.

<script>
  $('#target').change(function() {
    alert('Changed');
  });
</script>

Please help me figure out where I am doing wrong. In one of the similar problems on Stackoverflow, I got / as an answer. But even an exact copy paste of that code is not working for me.

P.S.: jQuery is loading as I tested it and found working using this:

<script>
$(function() {
  alert('Changed');
});
</script>

This question has already been asked but I am stuck with the most basic level of it. I haven't added anything to my html except a select tag and trying to catch the change event by jquery. Here is my code:

<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>

Even the change() function does not work.

<script>
  $('#target').change(function() {
    alert('Changed');
  });
</script>

Please help me figure out where I am doing wrong. In one of the similar problems on Stackoverflow, I got http://jsfiddle.net/78x9Q/4/ as an answer. But even an exact copy paste of that code is not working for me.

P.S.: jQuery is loading as I tested it and found working using this:

<script>
$(function() {
  alert('Changed');
});
</script>
Share Improve this question edited Sep 1, 2012 at 4:51 Danil Speransky 30.5k6 gold badges69 silver badges78 bronze badges asked Sep 1, 2012 at 4:19 ZeeshanZeeshan 5252 gold badges12 silver badges27 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 7

Your code does not work because you try to work with dom when it is not built yet. Use this:

$(document).ready(function () {
  // your code
});

Then your code will run when dom is already built.

This:

<script>
  $('#target').change(function() {
    alert('Changed');
  });
</script>

Ought to be this:

<script>
  $(function () {
    $('#target').change(function() {
      alert('Changed');
    });
  });
</script>

Meaning you are executing your bindings before the document is ready.

use it like this

$(document).ready(function () {
   $('#target').change(function() {
     alert('Changed');
   });
});

You need to wrap your jquery code inside document ready event

发布评论

评论列表(0)

  1. 暂无评论