views:

3360

answers:

7

I have a HTML form that contains text fields, checkboxes, radiobuttons and a submit button.

I'd like that submit button is enabled only if contents of fields are modified. Now here is the catch: if the user modifies field contents back to original values, the form button should be disabled again.

  1. Original field value "foo" => submit button is disabled
  2. User changes field value to "bar" => submit button becomes enabled
  3. User changes field value back to "foo" => submit button becomes disabled again

How to achieve this with jQuery? Are there any kind of general solution or script that I could use with any form?

Edit: What I am asking can't be done with simple dirty state tracking.

+2  A: 

Use dirty state tracking. Attach a boolean value (e.g. IsDirty) to every input control and toggle it whenever the value changes. While submitting the form check if atleast one or more values have changed and then submit the form. Otherwise display an alert to the user.

Another solution is to call a common function whenever a controls value changes. In this function you can set a global variable (IsDirty) to true if something changed and also enable/disable the submit button.

var isDirty = false;

function SomethingChanged(){
     if( !isDirty ) isDirty = true;
     btnSubmit.disabled = !isDirty;
}

Generic Function for any control

Assumptions: Add the initial value of each control to an attribute "InitVal"

function SomethingChanged(control){
     if( control.value != control.InitVal )
          control.IsDirty = true;
     else
          control.IsDirty = false;
}

In the above function to make it generic you can have separate functions for each type of control like TextBoxChanged and DropDownChanged etc. But have the following two attributes on each control

  1. InitValue - Initial Value of the control
  2. IsDirty - Boolean value indicating that the control's value has changed
Hemanshu Bhojak
This will not work if user changes value back to original, this approach would still have dirty bit set and I need the button become disabled in this case.
Juha Syrjälä
I have edited the answer check it out.
Hemanshu Bhojak
A: 

Have a look at the validation plug-in.

kgiannakakis
+2  A: 

Just two steps:

  1. store a hash of all initial values in a javascript variable
  2. onchange of every input recalculates that hash and verifies it with the one stored. if it's the same, disable the submit button, if it's not, enable it.
Kris
+1  A: 

Here's a plugin that tries to solve this problem

Juan Manuel
+1  A: 

This seems like a better answer.

1401-Using-jQuery-To-Leverage-The-OnChange-Method-Of-Inputs.htm

Nick
A: 

I havent tested whats below :-) But is that what you mean ?

$(document).ready(function() {
    $("#myform :input").attr("init",$(this).val()).bind("change.dirty", function(evt) {
        if ($(this).val()!=$(this).attr("init")) $(this).addClass("dirty");
        else $(this).removeClass("dirty");
        $('#thebutton').attr("disabled",!$("#myform .dirty").size());
    });
});

*-pike

pike
A: 

It should be possible to save the whole form object (either as it is, or by iterating with .each() and storing the data in a map), and then do the same onSubmit and compare both values.

yglodt