在JSP中使用Ajax的单个提交按钮提交两个HTML表单

gxwragnw  于 8个月前  发布在  其他
关注(0)|答案(3)|浏览(84)

我的网页设计是这样的,我必须使用两个形式,单击提交,然后保存到数据库,反之亦然。
我在一个带有Struts 2框架的JSP页面上使用了这个。我尝试过Ajax解决方案,但它们不适合我。

以下是我的脚本(已删除):

$("#visitType").buttonset();
$("#patientCondition").buttonset();
$("input[type=submit], a, button").button().click(function(event) {
  event.preventDefault();
  var inputs=$('#visitType,#patientCondition ').find(':input').not(this);
  var form_data={};
  inputs.each(function(){
    form_data[this.name]=$(this).val();
  });
  $.post('patientSoapAll',  form_data, function(response){  
  });
});

我的表单1:

<s:form action="PatientSoapAll" method="post">

        <div id="visitType">
            <input type="radio" id="I" value="I" <s:if test='pSB.rOS == "I"'>checked</s:if> name="pSB.rOS" /><label for="I">I V</label> 
            <input type="radio" id="R" value="R"<s:if test='pSB.rOS == "R"'>checked</s:if>name="pSB.rOS" /><label for="R">Regular   Visit</label> 
            <input type="radio" id="P/N" <s:if test='pSB.rOS == "P/N"'>checked</s:if>name="pSB.rOS" /><label for="D/N">Re- Evaluation</label> 
            <input type="radio" id="D/N"<s:if test='pSB.rOS == "D/N"'>checked</s:if> name="pSB.rOS" /><label for="
    
    P/N">Discharge</label>
    </div>
</s:form>

我的表单2:

<s:form action="PatientSoapAll" method="post">

    <div id="patientCondition">
        <input type="radio" id="new" value="n"<s:if test='pSB.r2 == "n"'>checked</s:if> name="pSB.r2" /> <label for="new">New</label>
        <input type="radio" id="noChange" value="nC" <s:if test='pSB.r2 == "nC"'>checked</s:if> name="pSB.r2" /><label for="noChange">No Change</label> 
        <input type="radio" id="progressing" value="p"<s:if test='pSB.r2 == "p"'>checked</s:if> name="pSB.r2" /><label for="progressing">Progressing</label> 
        <input type="radio" id="notProgressing" value="nP" <s:if test='pSB.r2 == "nP"'>checked</s:if>   name="pSB.r2" /><label for="notProgressing">Not Progressing</label>
    </div>
</s:form>

这是我的更新提交按钮。从<div>中删除并放在表中。

<tr>
  <td></td>
  <td><input type="submit" value="Save Note" /></td>
</tr>

我使用的是Struts 2,我想将这个提交按钮重定向到一个操作PatientSoapAll。我正在使用.do的自定义扩展而不是.action(默认情况下)。请告诉我如何将此提交按钮重定向到Struts 2中的相应操作类。

eoxn13cs

eoxn13cs1#

找到了一个简单的解决办法。
简体中文

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

提交按钮:

<input type="button" value="Save Note" onclick="submitForms()" />
72qzrwbm

72qzrwbm2#

你可以使用jQuery .serialize来做你想做的事情。给表单给予一些类,然后用它来选择表单,并调用serialize来获得标准URL编码符号的字符串。之后,你可以使用一些AJAX方法将其发布到你的操作中。

$(function() {
  $("#saveNoteButton").click(function() {
    alert($(".forms").serialize());
  });
});
fwzugrvs

fwzugrvs3#

您可以使用Struts2 jQuery提交多个表单

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

formIds

以逗号分隔的表单ID列表,在提交过程中单击此元素时将序列化所有字段(如果多个表单具有重叠的元素名称,则不确定将使用哪个)

相关问题