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

javascript - Listen to change events of multiple elements - Stack Overflow

programmeradmin0浏览0评论

I'm trying to listen to the change event of multiple input elements of different types at the same time.

This does not work:

$(function(){
  $(".update_search").change( function() {
    alert("do some");
  });
});

<input class="update_search" id="car_type_ids_" name="car_type_ids[]" type="checkbox" value="1">
<select id="size" name="search[size]">
  <option value="1">1</option>
  <option selected="selected" value="2">2</option>
</select>

The select and input tags are working fine, though I'm only receiving the change events on the check box.

EDIT: I'm calling jQuery after the DOM was rendered. Thus all events should bee bound.

I'm trying to listen to the change event of multiple input elements of different types at the same time.

This does not work:

$(function(){
  $(".update_search").change( function() {
    alert("do some");
  });
});

<input class="update_search" id="car_type_ids_" name="car_type_ids[]" type="checkbox" value="1">
<select id="size" name="search[size]">
  <option value="1">1</option>
  <option selected="selected" value="2">2</option>
</select>

The select and input tags are working fine, though I'm only receiving the change events on the check box.

EDIT: I'm calling jQuery after the DOM was rendered. Thus all events should bee bound.

Share Improve this question edited Sep 27, 2022 at 19:15 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 1, 2014 at 9:03 SebastianSebastian 1,6544 gold badges28 silver badges44 bronze badges 7
  • are you include jQuery library? – Pranav C Balan Commented Mar 1, 2014 at 9:04
  • Could you post rendered HTML instead?! Aren't you binding event before all elements are available in the DOM? – A. Wolff Commented Mar 1, 2014 at 9:04
  • Alright, wait a second. – Sebastian Commented Mar 1, 2014 at 9:07
  • Some input controls only fire a change event when the focus moves away from the element. – jfriend00 Commented Mar 1, 2014 at 9:09
  • Thanks, jfriend00, I'll try that out – Sebastian Commented Mar 1, 2014 at 9:10
 |  Show 2 more ments

1 Answer 1

Reset to default 5

try this..

$(function () {
       $("#car_type_ids_,#size").change(function () {
           alert("do some");
       });
   });

Or you can do this as...

<input class="update_search" id="car_type_ids_" name="car_type_ids[]" type="checkbox" value="1">
<select id="size" name="search[size]" class="update_search">
  <option value="1">1</option>
  <option selected="selected" value="2">2</option>
</select>



$(function () {
       $(".update_search").change(function () {
           alert("do some");
       });
   });
发布评论

评论列表(0)

  1. 暂无评论