简体   繁体   English

jQuery验证后,OnClick不起作用

[英]OnClick doesn't work after Jquery validations

I have given validations through jquery which works fine on button click. 我已经通过jquery给出了验证,该验证在单击按钮时效果很好。 But as soon as I fill all the data into the page, my onClick does not fires. 但是,一旦我将所有数据填充到页面中,我的onClick不会触发。 I dont know why. 我不知道为什么。 Please suggest. 请提出建议。

Here is my jquery code:- 这是我的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');
            }
        }
    }); 

Also see my aspx of button:- 另请参阅按钮的aspx:-

<asp:Button ID="btnSave" CssClass="button" Text="Save" runat="server" 
                    CausesValidation="true" onclick="btnSave_Click"
                    />

I suggest you do NOT use the click of the button but the submit of the form to validate: 我建议您不要单击按钮,而要提交表单来验证:

$(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 = [];
        }
    });
.
.
.

I assume that also means you should make your button a submit button and make sure the asp does not add a click event since it is already assigned by the jQuery 我认为这也意味着您应该将按钮设为“提交”按钮,并确保asp不添加click事件,因为它已由jQuery分配。

$('#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;
    }
});

move event prevent on the button click handler only when client validation fails. 仅当客户端验证失败时,才在按钮单击处理程序上执行move事件阻止。

    $('#btnSave').click(function (e) {

        validateTitle();
        validateddl();
        validateTextBoxes();
        if (ErrArr.length > 0) {
            alert(ErrArr.join("\n"));
            ErrArr = [];
            e.preventDefault();
            return false;
        }
    });

Try this 尝试这个

$('#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;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM