1

我在捕捉选择的更改事件时遇到问题。如果选择器直接指向 id,则没有问题。但是,它不起作用。你知道为什么吗?

这是我的一段代码:

<html>
<head><title></title>

  <link rel=stylesheet href="css/jquery-ui-1.9.2.custom.css" />
  <script type="text/javascript" src="js/jquery-1.9.1.min.js"> </script>
  <script src="js/jquery-ui-1.9.2.custom.js"></script>


....

  <select name="type_book_1" id="type_book_1">
    <option value="ae">airplane</option>
    <option value="ca">car</option>
   </select>

....

JS:

<script>
$(function () {
    $(document.body).on('change', 'input[name^="type_book"]', function () {
        alert('Change Happened');
    });
});
</script>
4

4 回答 4

1

selectinput

  $(document).on('change','select[name^="type_book"]',function(){
                            // ^^^^here write select at the place of input
                                alert('Change Happened');
                });

演示

于 2013-11-07T12:33:35.593 回答
1

元素选择器input错误,应该是select

$(document.body).on('change', 'select[name^="type_book"]', function () {
    alert('Change Happened');
});

演示:小提琴

于 2013-11-07T12:33:37.817 回答
1

You can do this:

$(document.body).on('change', ':input[name^="type_book"]', function () {
    alert('Change Happened');
});

Here, the :input selector will select all input, textarea, select and button elements.

But, in your case you are using the input selector which only selects the input type elements only, not the select elements. Hence, your code is not working.

于 2013-11-07T12:37:59.227 回答
0

小提琴演示

选择器input错误,将其更改为select

$(document.body).on('change', 'select[name^="type_book"]', function () {
    alert('Change Happened');
});
于 2013-11-07T12:34:10.580 回答