有条件地创建 ajax url 参数以与 jQuery 验证一起使用

Conditionally create ajax url parameter to use with jQuery Validate

我的 jquery 验证脚本有问题。本质上,我需要的是:根据字符串的最后一部分 (isAdd) 中的内容,当用户单击发送按钮时,脚本将在 if 或 else 语句之间进行选择,并发送一个 ajax 请求到验证输入字段。服务器将发送一个 true 或 false 标志。如果为 true,则提交表单,如果为 false,则不提交表单。我认为我的代码显示得更好:

// Waiting for the btnSave to be clicked
$(document).on('click', '.btnSave', function (event) {

    // Here I get the action value in the form tag, split it in chunks and 
    // get the last element in the array, assigned it to the isAdd variable
    var isAdd = $('#customerAddressForm').prop("action").split("/").pop();

    // If the variable value is the same as "Shipping", it means that it's 
    // the edit button that was clicked
    if (isAdd == "Shipping") {

        // I add the following rules to the input with id "inputAddressName"
        $('input[id="inputAddressName"]').rules("add", {
            required: true,
            noSpace: true,

            // Here I do the Ajax call, the value in the customerAddressId 
            // is appended to the url. Example: 
            // https://localhost:8443/mysite/useraccount/isExistingAddressName?addressName=Bill1&customerAddressId=1232. 
            // This get a true/false flag from the server

            remote: {
                url: "https://localhost:8443/mysite/usersite/addresses/isExistingAddressName",
                contentType: "application/json; charset=utf-8",
                data: {
                    customerAddressId: function () {
                        return $("#customerAddressForm").prop("action").split("/").pop();
                    }
                }
            }
        });

    // If the variable value is not the same as "Shipping", it means that 
    // it's the add button that was clicked
    } else {
        $('input[id="inputAddressName"]').rules("add", {
            required: true,
            noSpace: true,

            // Here I do the Ajax call for the add, a hardcoded value (-10) 
            //for the customerAddressId is appended to the url. Example: 
            // https://localhost:8443/mysite/useraccount/isExistingAddressName?addressName=Bill1&customerAddressId=-10. 
            //This get a true/false flag from the server.
            remote: {
                url: "https://localhost:8443/mysite/usersite/addresses/isExistingAddressName",
                contentType: "application/json; charset=utf-8",
                data: {
                    customerAddressId: function () {
                        return $("-10");
                    }
                }
            }
        });
    }


    $('#customerAddressForm').validate({

        ignore: 'input[type=hidden], .select2-input, .select2-focusser',
        rules: {
            "address.addressLine1": {
                required: true
            },
            "address.addressLine2": {
                number: true
            }
        },
        messages: {
            "addressName": {
                required: "Please enter an address name",
                remote: "Address name already taken. Please enter another one"
            },
                "address.addressLine1": {
                required: "Please enter your street address"
            },
                "address.addressLine2": {
                number: "Please enter only numbers"
            }
        },
        highlight: function (element) {
            $(element).closest('.control-item').addClass('has-error');
        },
        unhighlight: function (element) {
            $(element).closest('.control-item').removeClass('has-error');
        },
        errorElement: 'small',
        errorClass: 'help-block',
        errorPlacement: function (error, element) {
            error.insertAfter(element);
        }
    });
});

这是根据 if/else 语句在远程选项中传递 url 的正确方法吗?它不起作用,我得到: Uncaught TypeError: Cannot read 属性 'settings' of undefined.

您的代码...

$(document).on('click', '.btnSave', function (event) {
    ....
    if (isAdd == "Shipping") {
        $('input[id="inputAddressName"]').rules("add", {
            ....
        });
    } else {
        $('input[id="inputAddressName"]').rules("add", {
            ....
        });
    }

    $('#customerAddressForm').validate(....);  // <-initializes plugin

});

I only get "Cannot read property 'settings' of undefined"

您的代码中有一些致命错误...

  1. 您在表单上初始化插件之前尝试使用.rules()方法;这可能是您的错误消息的来源。 .validate()方法仅用于插件的初始化

  2. 由于 .validate() 方法用于在您的表单上初始化插件,并且只被调用 一次(在 DOM 就绪事件中),你 永远不会 将它放在 click 处理程序中。

改为这样做...

$(document).ready(function() {

    $('#customerAddressForm').validate(....); // <-initializes plugin

    $(document).on('click', '.btnSave', function (event) {
        ....
        if (isAdd == "Shipping") {
            $('input[id="inputAddressName"]').rules("add", {
                ....
            });
        } else {
            $('input[id="inputAddressName"]').rules("add", {
                ....
            });
        }
    });

});