jQuery对话框保存按钮点击后,提交表单没有反应
<p>我在JSP中有一个表单和JavaScript的以下代码。我面临的问题如下:</p>
<p>当我点击保存按钮时,如果满足<code>if(speciesid === 2){</code>条件,并且我从jquery对话框中点击<code>"保存"</code>按钮,它不会触发控制器。然而,只要<code>if(speciesid === 2){</code>条件不满足,它就能正常工作并触发我的控制器。我对这一行<code>" document.getElementById("orderForm").submit();"</code>有些怀疑。有人能看出我的代码有什么问题吗?为了避免代码内容过多,其他表单字段已被省略。</p>
<pre class="brush:php;toolbar:false;"><form:form method="POST" modelAttribute="orderForm"
onsubmit="return validateOrderForm(this)">
<form:errors path="*" cssClass="errorblock" element="div" />
<form:hidden path="choiceBstatus" />
<c:if test="${Species.speciesID == 2}">
<div id="checklist_dialog" title="New Entry">
<form>
<p>
Name:
<input type="text" name="name" />
</p>
<p>Group:
</p>
</form>
</div>
</c:if>
<table class="noPrint">
<tr>
<td align="center"><br /> <input onclick="formSaveButton()" class="button" type="submit"
name="save" value="Save" /> - <input class="button" type="submit"
name="cancel" value="Cancel" onclick="bCancel = true;" /></td>
</tr>
</table>
</form:form>
<script type="text/javascript">
$('#checklist_dialog').hide();
function formSaveButton(){
getSpeciesID('<c:out value="${Species.speciesID}"/>');
getChoiceBStatus('<c:out value="${orderForm.choiceBstatus}"/>');
}
function getSpeciesID(speciesid){
console.log("Species ID");
console.log(speciesid);
if(speciesid === 2){
var selectedVal = "";
var selected = $("input[type='radio'][name='sampleChoice']:checked");
if (selected.length > 0) {
selectedVal = selected.val();
console.log(selectedVal);
}
$('#checklist_dialog').dialog({
modal: true,
overlay: {
opacity: 0.7,
background: "black"
},
buttons: {
"SAVE": function() {
$(this).dialog('close');
$("#choiceBstatus").val("true");
document.getElementById("orderForm").submit();
},
"CANCEL": function() {
$(this).dialog('close');
alert("You must complete / save the checklist before saving your form!");
$("#choiceBstatus").val("false");
}
}
});
}
}
function getChoiceBStatus(status){
console.log("Choice B Status");
console.log(status);
}
</script></pre>
<p><br /></p>
从我所看到的情况来看,您在表单上缺少了id,
document.getElementById("orderForm")
通过选择具有匹配id的HTML元素来工作,在这里是"orderForm"
。然而,您的HTML表单没有指定任何id。 请尝试更新表单以包括以下id。