common event for all elements on form

2019-07-19 16:39发布

I want to fire a common event whenever a value of any element in form changes. On the basis of values of form elements i want implement code.

i am writing below code but onchange event is not working on formid.

$("#formid").change(function () {
    //code 
}); 

or this

$("#formid").bind("change", function () {
    //code 
});

How to fire event on form whenever there is any change in element's value ?

4条回答
三岁会撩人
2楼-- · 2019-07-19 17:14

You can bind change event for all necessary form controls

$('input, select, textarea', $('#formid')).change(function()
{
    //your code here
});
查看更多
来,给爷笑一个
3楼-- · 2019-07-19 17:23
$(this).toArray().each(function(index, element){
    $(element).change(function(){.....});

});

put this in document.ready...

查看更多
贼婆χ
4楼-- · 2019-07-19 17:29

Loop through all elements of your document or form and set their onChange event to your handling function. Like:

for (var i = 0; i < yourelements.length; i++)
yourelements[i].onchange = change;
查看更多
【Aperson】
5楼-- · 2019-07-19 17:40

How to fire event on form whenever there is any change in element's value ?

Just do as it looks like you've done. Bind the change event to the form element.

$("form").change(function() {
   // Some descendant changed.
});

So long as you're not interfering with the propagation of the events, this will work.

查看更多
登录 后发表回答