我有两种类型的提交,一种是必需的,另一种是不需要的。
如果用户按下提交按钮,那么我调用一个功能,我有设置逻辑用于检查
<script language="javascript" type="text/javascript">
function CheckEmptyField() {
var isPageValid;
if (typeof (Page_ClientValidate) == 'function') {
isPageValid = Page_ClientValidate();
if (isPageValid) {
var elementNotRequired = document.getElementsByTagName("span")
var strvalidationMessage = "<h5>You haven’t entered the following columns:</h5><br/><ul>";
for (var ele = 0; elementNotRequired.length; ele++) {
if (elementNotRequired[ele].className == 'NotRequired') {
strvalidationMessage += "<li>" + elementNotRequired[ele].innerText + "</li>";
}
}
strvalidationMessage += "</ul><br/> <h6>Do you want to enter these details?</h6>";
isPageValid = confirm(strvalidationMessage);
}
}
return isPageValid;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="1" cellspacing="1" width="650px">
<tr>
<td colspan="2">
Following fields are <b>Required!</b>
</td>
<td colspan="2">
Following fields are <b>Not Required!</b>
</td>
</tr>
<tr>
<td>
First Name
</td>
<td>
<asp:TextBox runat="server" ID="txtFirstName"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtFirstName"
ErrorMessage="First Name is required" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
<td>
Education
</td>
<td>
<asp:TextBox runat="server" ID="txtEducation"></asp:TextBox>
<span class="NotRequired">Education</span>
</td>
</tr>
<tr>
<td>
Last Name
</td>
<td>
<asp:TextBox runat="server" ID="txtLastName"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtLastName"
ErrorMessage="Last Name is required" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
<td>
Company Name
</td>
<td>
<asp:TextBox runat="server" ID="txtCompanyName"></asp:TextBox>
<span class="NotRequired">Company Name</span>
</td>
</tr>
<tr>
<td>
Age
</td>
<td>
<asp:TextBox runat="server" ID="txtAge"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtAge"
ErrorMessage="Age is required" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClientClick="return CheckEmptyField();" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" />
</td>
<td colspan="2">
</td>
</tr>
</table>
</div>
答案 0 :(得分:1)
“for”循环中存在语法错误。将循环的开始更改为以下内容:
for ( var ele = 0; ele < elementNotRequired.length; ele++ )
通过此更改以及您具有功能逻辑的方式,用户将收到确认提示,如果用户单击“取消”,则页面将不会回发。
答案 1 :(得分:0)
您可以在提交表单时调用js方法的修订版本,该版本应在验证触发后发生。如果用户想要回答不需要的值,请修改您的js以取消提交。