Submit two HTML forms in JSP using single submit button with Ajax

前端 未结 3 1122
忘掉有多难
忘掉有多难 2021-01-15 06:20

My page design is such that I have to use two forms that submit on single click and then saved to database and vice verse. I am using this on a JSP page with Struts2 Framewo

相关标签:
3条回答
  • 2021-01-15 07:04

    You can use jQuery .serialize to do what you want. Give some class to your forms then use it to select your forms and call serialize to get a string in the standard URL-encoded notation. After that you can use some AJAX method to post it to your action.

    $(function() {
      $("#saveNoteButton").click(function() {
        alert($(".forms").serialize());
      });
    });
    
    0 讨论(0)
  • 2021-01-15 07:07

    Found an easy solution to it.

    javascript:

    submitForms = function() {
      $('#form2 :input').not(':submit').clone().hide().appendTo('#form1');
        document.getElementById("form1").submit();
    };
    

    Submit button :

    <input type="button" value="Save Note" onclick="submitForms()" />
    
    0 讨论(0)
  • 2021-01-15 07:08

    You can use Struts2 jQuery to submit multiple forms

    <sj:submit formIds="visitType,patientCondition" targets="result" value="Submit" />  
    

    formIds:

    Comma delimited list of form ids for which to serialize all fields during submission when this element is clicked (if multiple forms have overlapping element names, it is indeterminate which will be used)

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