common event for all elements on form

2019-07-19 16:52发布

问题:

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 ?

回答1:

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.



回答2:

You can bind change event for all necessary form controls

$('input, select, textarea', $('#formid')).change(function()
{
    //your code here
});


回答3:

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;


回答4:

$(this).toArray().each(function(index, element){
    $(element).change(function(){.....});

});

put this in document.ready...