JaneTho JaneTho - 3 years ago 91
jQuery Question

Jquery: Disable button if there is no change in form

Goal: disable button if there is no changes in form.

I already have a working code for this function actually, the only problem is I can't get it work if have a

input[type=file]
in my form. If I only change the file the button is still disabled.

Would you help me please?

Here's what I've done so far

$('#formEdit').each(function(){
$(this).data('serialized', $(this).serialize());
}).on('change input', function(){
$(this).find('input:submit').attr('disabled', $(this).serialize() == $(this).data('serialized'));
});


Thank you.

Answer Source

The .serialize() method not work with file input types so you can use the new FormData('form'); Below is the working code for your use case :

  $('#formEdit').each(function() {
    var form_data = new FormData('form');
    $(this).data('serialized', form_data);
}).on('change input', function() {
    var form_data1 = new FormData('form');
    $(this).find('input:submit').attr('disabled', form_data1 == $(this).data('serialized'));
});

Please refer jquery doc to explore more on .serialize() method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download