Merge values from two forms on submit

前端 未结 9 924
甜味超标
甜味超标 2020-11-29 18:12

I have two forms on one html page. Using jQuery, is it possible to have the data from both forms go into the POST data when the first is submitted?

9条回答
  •  醉话见心
    2020-11-29 18:48

    One approach is to copy all of form2's inputs into form1 once the data validation check succeeds. This assumes you are not doing an AJAX submit.

    // new onsubmit function for form1
    function form1_onsubmit()
    {
        if (!EntryCheck()) return false; 
    
        $('#form2 :input').not(':submit').clone().hide().appendTo('#form1');
    
        return true;
    }
    

    If you wanted to cater for hitting submit twice, possibly because of submit fail from the server, we would need to remove any copied inputs before copying in new ones.

    // new onsubmit function for form1
    function form1_onsubmit()
    {
        $('#form1 :input[isacopy]').remove();
    
        if (!EntryCheck()) return false; 
    
        $('#form2 :input').not(':submit').clone().hide().attr('isacopy','y').appendTo('#form1');
    
        return true;
    }
    

提交回复
热议问题