我通过jquery给出了验证,它在按钮点击时工作正常。
但是只要我将所有数据填入页面,我的onClick
就不会触发。我不知道为什么。请建议。
这是我的jquery代码: -
var ErrArr = [];
$(document).ready(function () {
$('#btnSave').click(function (e) {
e.preventDefault();
validateTitle();
validateddl();
validateTextBoxes();
if (ErrArr.length > 0) {
alert(ErrArr.join("\n"));
ErrArr = [];
return false;
}
});
function validateTitle() {
if ($("#ddlTitle").val() > "0") {
if ($("#ddlTitle").val() == "1104" && $("#txtTitle").val() === "") {
ErrArr.push("Please enter the text in other title");
}
} else {
ErrArr.push('Please select the title');
}
}
function validateTextBoxes() {
if ($("#txt_emp_fname").val() === "") {
ErrArr.push('First name is required');
}
if ($("#txt_emp_mname").val() === "") {
ErrArr.push('Middle name is required');
}
if ($("#txt_emp_lname").val() === "") {
ErrArr.push('Last name is required');
}
if ($("#txtDateofJoin").val() === "") {
ErrArr.push('Date of joining is required');
}
if ($("#txtReligion").val() === "") {
ErrArr.push('Religion is required');
}
if ($("#txtBloodGroup").val() === "") {
ErrArr.push('Blood group is required');
}
if ($("#txtPersonalEmail").val() === "") {
ErrArr.push('Email Id is required');
}
if ($("#txtDtOfBirth").val() === "") {
ErrArr.push('Date of birth is required');
}
if ($("#txtAt").val() === "") {
ErrArr.push('Place of birth is required');
}
if ($("#txtTaluka").val() === "") {
ErrArr.push('Taluka is required');
}
if ($("#txtPostOffice").val() === "") {
ErrArr.push('Post office is required');
}
if ($("#txtDistrict").val() === "") {
ErrArr.push('District is required');
}
if ($("#txtStatePersonal").val() === "") {
ErrArr.push('State is required');
}
if ($("#txtDtMarriage").val() === "") {
ErrArr.push('Date of Marriage is required');
}
if ($("#TxtPassportNo").val() === "") {
ErrArr.push('Passport no is required');
}
if ($("#txtIdMark").val() === "") {
ErrArr.push('Identification mark is required');
}
}
function validateddl() {
if ($("#ddlGender").val === "" || $("#ddlGender").val() == "0") {
ErrArr.push('Please select the gender');
}
if ($("#ddlMaritalStatus").val === "" || $("#ddlMaritalStatus").val() == "0") {
ErrArr.push('Please select the Marital Status');
}
if ($("#ddlNationality").val === "" || $("#ddlNationality").val() == "0") {
ErrArr.push('Please select the Nationality');
}
}
});
另见我的aspx按钮: -
<asp:Button ID="btnSave" CssClass="button" Text="Save" runat="server"
CausesValidation="true" onclick="btnSave_Click"
/>
答案 0 :(得分:3)
我建议您不要使用按钮的单击,而是使用表单提交来验证:
$(document).ready(function () {
$('#formID').on("submit",function (e) {
// e.preventDefault(); // does not belong here
validateTitle();
validateddl();
validateTextBoxes();
if (ErrArr.length > 0) {
e.preventDefault();// but here
alert(ErrArr.join("\n"));
ErrArr = [];
}
});
.
.
.
我认为这也意味着你应该让你的按钮成为一个提交按钮,并确保asp不添加点击事件,因为它已经由jQuery分配
答案 1 :(得分:1)
$('#btnSave').on("click", function(e) {
//e.preventDefault();
validateTitle();
validateddl();
validateTextBoxes();
if(ErrArr.length > 0) {
e.preventDefault(); //use e.preventDefault here
alert(ErrArr.join("\n"));
ErrArr = [];
//return false;
}
});
答案 2 :(得分:0)
仅在客户端验证失败时才在按钮单击处理程序上移动事件阻止。
$('#btnSave').click(function (e) {
validateTitle();
validateddl();
validateTextBoxes();
if (ErrArr.length > 0) {
alert(ErrArr.join("\n"));
ErrArr = [];
e.preventDefault();
return false;
}
});
答案 3 :(得分:0)
试试这个
$('#btnSave').click(function(e) {
//e.preventDefault();
validateTitle();
validateddl();
validateTextBoxes();
if(ErrArr.length > 0) {
e.preventDefault(); //use e.preventDefault here
alert(ErrArr.join("\n"));
ErrArr = [];
//return false;
}
});