开发者

How to disable onChange for a number of elements, change then, then enable onChange in jQuery

I have a bunch of input elements on a page. Each input element has an onchange event that triggers an ajax call to the server. I want to disable all these onchanges then make som开发者_开发百科e programatic changes to the elements (reset them), then re-enable these onchange events. I want only one ajax call to happen as a result of this.

How do I do this using JQuery?

Many Thanks!


Changing an input elements value through .val() doesn't trigger a change event so you can set your values and then trigger the change event at the end by hand:

$('#a').val('where is');
$('#b').val('pancakes house?');
$('#a').change(); // This is the only change event

Demo: http://jsfiddle.net/ambiguous/Qd2Ad/

From the fine manual:

change
The change event occurs when a control loses the input focus and its value has been modified since gaining focus. This event is valid for INPUT, SELECT, and TEXTAREA. element.

There is no focus change when you just call val so there is no event.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜