Find out if html form has changed

前端 未结 6 2121
清酒与你
清酒与你 2020-12-08 11:53

Using jquery I\'ve added a change handler to a form. This works when any input is changed BUT only if the user manually changes an input and not when some other code changes

相关标签:
6条回答
  • 2020-12-08 12:00

    This is easily achieved in JavaScript without jQuery. initChangeDetection() can be called multiple times:

    function initChangeDetection(form) {
      Array.from(form).forEach(el => el.dataset.origValue = el.value);
    }
    function formHasChanges(form) {
      return Array.from(form).some(el => 'origValue' in el.dataset && el.dataset.origValue !== el.value);
    }
    

    Test on JS Bin


    For older browsers that don't support newer arrow/array functions:

    function initChangeDetection(form) {
      for (var i=0; i<form.length; i++) {
        var el = form[i];
        el.dataset.origValue = el.value;
      }
    }
    function formHasChanges(form) {
      for (var i=0; i<form.length; i++) {
        var el = form[i];
        if ('origValue' in el.dataset && el.dataset.origValue !== el.value) {
          return true;
        }
      }
      return false;
    }
    
    0 讨论(0)
  • 2020-12-08 12:10

    Yes, there seems to be some confusion over this. In an ideal world you would expect the onchange event to happen whenever the inputs change but thats not what happens. I'm sure for good reasons to - maybe not.

    One way I've overcome this obstacle is to capture the form state into a variable just after displaying it and then just before submitting it to check if the state has changed and to act accordingly.

    An easy state to store is what the serialize function returns. An easy place to store the state is using the data functionality. Both serialize and data are available with jquery.

    Of course you can use other different forms of state (some form of hash) or storage for this state (standard global variable for example).

    Here is some prototype code:

    If your form id is 'xform' then you can call the following code when the form has displayed:

    $('#xform').data('serialize',$('#xform').serialize());
    

    And then, when you need to check, for example just before a button submit you can use:

    if($('#xform').serialize()!=$('#xform').data('serialize')){
        // Form has changed!!!
    }
    

    You could wrap all this up into a copy & paste javascript snippet that will give you a formHasChanged() function to call wherever you need it (NOT TESTED):

    $(function() {
        $('#xform').data('serialize',$('#xform').serialize());
    });
    function formHasChanged(){
        if($('#xform').serialize()!=$('#xform').data('serialize')){
            return(true);
        }
        return(false);
    }
    

    But I'll stop here otherwise I'll create yet another jquery plugin.

    0 讨论(0)
  • 2020-12-08 12:11

    Not in a regular way.

    You can change with input and then trigger the change event.

    $('#inputId').val('foo').trigger('change');
    

    or with this:

    $('#inputId').val('foo').change();
    
    0 讨论(0)
  • 2020-12-08 12:19

    Serializing the form is certainly an option, but it will not work if:

    • you want to know which fields have changed
    • it only needs to check a subset of the fields
    • dynamically adding or removing fields.

    Fortunately, every form element has a default value associated with its object:

    • input, textarea : defaultValue
    • checkbox, radio : defaultChecked
    • select: defaultSelected

    for ex: to ckeck if input or textarea has changed:

    var changed = false;
    $(":text,textarea").each(function(){
        changed = this.value != this.defaultValue;
        return !changed; // return if at least one control has changed value
    });
    
    0 讨论(0)
  • 2020-12-08 12:21

    Here is what i did (i found my solution using zaf's answer)

    $("form").change(function() {
        $(this).data("changed","true");
    });
    
    $("input[type='submit']").click(function() {
        if($("form").data("changed") == "true") {
            var discard = confirm("Some unsaved changes. Discard them ?");
            if(!discard) return false;
        }
    });
    
    0 讨论(0)
  • 2020-12-08 12:25

    Try onchange attribute According to W3c it should trigger anytime the content of an element, the selection, or the checked state have changed.

    0 讨论(0)
提交回复
热议问题