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

jquery - javascript text input change event not firing - Stack Overflow

programmeradmin3浏览0评论

I have a text type input field and a checkbox. If I change the text and then click outside the input box (or press enter or tab) the change event is thrown. But if I enter some text and then click directly on the checkbox using the mouse, only the checkbox change event seems to be thrown.

I have the following code:

<input type="text" name="text" class="update">
<input type="checkbox" name="check" class="update">

and this jQuery:

$('.update').change(function(){
    console.log($(this));
});

Is this a known problem, and how can I make sure all change events are fired/thrown/caught in this setup?

I have a text type input field and a checkbox. If I change the text and then click outside the input box (or press enter or tab) the change event is thrown. But if I enter some text and then click directly on the checkbox using the mouse, only the checkbox change event seems to be thrown.

I have the following code:

<input type="text" name="text" class="update">
<input type="checkbox" name="check" class="update">

and this jQuery:

$('.update').change(function(){
    console.log($(this));
});

Is this a known problem, and how can I make sure all change events are fired/thrown/caught in this setup?

Share Improve this question asked Jan 13, 2011 at 21:52 mikkelbreummikkelbreum 3,08110 gold badges39 silver badges41 bronze badges 5
  • Is that code in a $(document).ready(function()); function? You could try putting an alert() in the function to see if you're hitting the function. – codersarepeople Commented Jan 13, 2011 at 21:55
  • Strange. Something else must be going on because my test works clicking from a changed text input to the checkbox jsfiddle/tbeseda/UCtyb – tbeseda Commented Jan 13, 2011 at 21:58
  • Are you sure? I copied the HTML/JS and could see both the events firing when I type something in textbox and then click on checkbox. – Chandu Commented Jan 13, 2011 at 21:59
  • You are calling same event at the same time, so one of this calls will respond, just change the event on the text box and call it with jQuery separately – Mohammad Commented Jan 13, 2011 at 22:01
  • OK, so something else must be going on, you're right.. I'll make more tests. Actually I use the events to fire an ajax call, and only one of these ajax calls seems to be executed.. so I 'thought' it was the event not firing, but the problem may lie elsewhere.. – mikkelbreum Commented Jan 16, 2011 at 21:05
Add a ment  | 

4 Answers 4

Reset to default 2

To fire user changes, use the input event:

$('input').on('input',function(){...})

To fire code changes, use the DOMSubtreeModified event:

$('input').bind('DOMSubtreeModified',function(){...})

If you want to fire both user and code changes:

$('input').bind('input DOMSubtreeModified',function(){...})

The DOMSubtreeModified event is marked as deprecated and sometimes quite CPU time consuming, but it may be also very efficient when used carefully...

I'm not sure if I get it. But for me when I try to type in textfield and then click checkbox by mouse both events are fired. But you have to keep in mind that event 'change' for text input means that this input has to loose focus, as long as field is focused no change event ever will be triggered. This somehow might be your case. Checkboxes/radioboxes work different way tho. No need to loose focus.

Cheers.

P.S. My test case: http://dl.dropbox./u/196245/index16.html

The change event fires for both because you're listening to the update class.

The change event will not fire unless the input focus switched to other controls

发布评论

评论列表(0)

  1. 暂无评论