0

我在验证表单 onclick 事件时遇到问题。

我正在使用一个表单,但它不包含任何按钮或提交事件。我使用的是按钮而不是按钮,那么当用户单击时<a>我该如何管理?.valide()<a> </a>

这是用户单击以提交表单的表单链接代码:

<a  style="cursor: pointer" class="edit_submit_btn"><?php echo "Update"; ?></a>

jquery代码(工作正常):

$(document).on('click','.edit_submit_btn',function(){

)};

但是当我尝试使用 验证表单时.validate,它不起作用。

这里的验证代码:

$('.user_edit').validate({

    onkeyup: false,
    errorClass: 'has-error-text',
    validClass: 'valid',
    rules: {
        "data[PersonMaster][first_name]": {
            required: true
        },
        "data[PersonMaster][last_name]": {
            required: true
        },
        "data[PersonMaster][email_address]": {
            required: true,
            email: true
        },
        "data[PersonMaster][mobile_number_code]": {
            required: true
        },
        "data[PersonMaster][mobile_number]": {
            required: true
        },
        "data[PersonMaster][country_id]": {
            required: true
        }
    },
    messages: {
        "data[PersonMaster][first_name]": {
            required: globalValidations.Modules.addUser.first_name
        },
        "data[PersonMaster][last_name]": {
            required: globalValidations.Modules.addUser.last_name
        },
        "data[PersonMaster][email_address]": {
            required: globalValidations.Modules.addUser.email_address,
            email:globalValidations.Modules.addUser.email_valid
        },
        "data[PersonMaster][mobile_number_code]": {
            required: globalValidations.Modules.addUser.mobile_number_code
        },
        "data[PersonMaster][mobile_number]": {
            required: globalValidations.Modules.addUser.mobile_number
        },
        "data[PersonMaster][country_id]": {
            required: globalValidations.Modules.addUser.country_code
        }
    },
    highlight: function(element) {
        alert("hi");

        $(element).closest('div').addClass("has-error");
    },
    unhighlight: function(element) {

        $(element).closest('div').removeClass("has-error");
    },
    errorPlacement: function(error, element) {

        $(element).closest('div').append(error);
    },
    submitHandler: function(form) {


        $("#loading_main").show();
        var data = $(form).serialize();
         var page_number = $("#page_value").val();

        $.ajax({
            url: site_url + "user_masters/edit_action",
            data: data,
            type: "POST",
            success: function(response) {

                $("#loading_main").hide();
                var response_object = jQuery.parseJSON(response);
                if (response_object.error_code == 0) {
                     UserloadPiece(site_url + "user_masters/user_list/" + (new Date()).getTime() + '/page:' + page_number + '', ".user-content-table-inner", '');
                //                        setTimeout(message_hide("alert", "", "alert-success"), 5000);
                } else if (response_object.error_code == '1') {
                    $("#alert-edit-profile").show();
                    $("#alert-edit-profile").removeClass("alert-success");
                    $("#alert-edit-profile").addClass("alert-danger");
                    $("#alert-edit-profile").html(response_object.message);
                } else if (response_object.error_code == '2') {
                    var i = 0;
                    $.each(response_object.message, function(field, error) {
                        if (field != "null") {
                            $(".has-error-text").empty();

                            $("#" + field + "_validation").closest('div').addClass("has-error");
                            $("#" + field + "_validation").text(error);
                            $("#" + field + "_validation").show();
                            if (i == 0)
                                $("#" + field).focus();
                            i = i + 1;
                        }
                    });
                }
                else if(response_object.error_code == '9'){
                    handle_redirect();
                }
            }
        });
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert("There is some error while processing your request. please try again later.");
        window.location.href = site_url + 'user_masters/login';
    }
});
4

1 回答 1

0

只需使用表单 ID 来验证表单

$('#form_id').validate();

并在输入字段中添加属性

data-rule-required=true

如果您想要自定义消息,只需添加另一个属性

data-msg-required="Your message"

于 2015-09-10T15:10:02.707 回答