[removed] True form reset for hidden fields

前端 未结 10 1656
臣服心动
臣服心动 2020-12-14 17:12

Unfortunately form.reset() function doesn\'t reset hidden inputs of the form. Checked in FF3 and Chromium.

Does any one have an idea how to do the reset for hidden f

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 17:39

    Another answer, in case anyone comes here looking for one. Serialize the form after the page loads and use those values to reset the hidden fields later:

    var serializedForm = $('#myForm').serialize();
    

    Then, to reset the form:

    function fullReset(){
    
        $('#myForm').reset(); // resets everything except hidden fields
    
    
        var formFields = decodeURIComponent(serializedForm).split('&'); //split up the serialized form into variable pairs
    
        //put it into an associative array
        var splitFields = new Array();
        for(i in formFields){
            vals= formFields[i].split('=');
            splitFields[vals[0]] = vals[1];
        }
        $('#myForm').find('input[type=hidden]').each(function(){    
            this.value = splitFields[this.name];
        });
    
    }
    

提交回复
热议问题