开发者

When JQuery change function is triggered

开发者 https://www.devze.com 2023-03-13 01:39 出处:网络
I am using Jquery to set some flag whenever input elements value get changed. I wrote a simple script whic开发者_StackOverflow中文版h is working fine but I observed that the function get called only

I am using Jquery to set some flag whenever input elements value get changed.

I wrote a simple script whic开发者_StackOverflow中文版h is working fine but I observed that the function get called only when control get out of the input element.If user types something and keeps the control in the same input then function is not called.

I want to execute the function whenever input elements value get changed irrespective of where the control is.

Here is my code

$("input,textarea").live('change',function ()
{
   // Set flag
});

I am using live() to bind change() method to dynamicaly added input elements.

~ Ajinkya.


Semantically, the change event is triggered when the element loses focus. That's when its value property is updated.

If you want to work whenever a key is pressed, use the keypress event:

$("input,textarea").live('keypress',function ()
{
   // Set flag
});


Use keyup/keydown event instead of change:

$("input,textarea").live('keyup',function ()
{
   // Set flag
});


The 'change' event works as lonesomeday said. But if you want to set your flag whenever something occurs, you can create your own event. For instance, with your custom event, you can set your flag if the user change the field, press a key, focus on the element or anything you want.

To know more about that: http://brandonaaron.net/blog/2009/06/4/jquery-edge-new-special-event-hooks

0

精彩评论

暂无评论...
验证码 换一张
取 消