Javascript 有条件地创建用于jQuery验证的ajax url参数
我的jquery验证脚本有问题。本质上,我需要的是:根据字符串最后一部分(isAdd)中的内容,当用户单击send按钮时,脚本将在if或else语句之间进行选择,并发送ajax请求以验证输入字段。服务器将发送true或false标志。如果为true,则提交表格;如果为false,则不提交表格。我认为我的代码更好地显示了这一点:Javascript 有条件地创建用于jQuery验证的ajax url参数,javascript,jquery,ajax,jquery-validate,Javascript,Jquery,Ajax,Jquery Validate,我的jquery验证脚本有问题。本质上,我需要的是:根据字符串最后一部分(isAdd)中的内容,当用户单击send按钮时,脚本将在if或else语句之间进行选择,并发送ajax请求以验证输入字段。服务器将发送true或false标志。如果为true,则提交表格;如果为false,则不提交表格。我认为我的代码更好地显示了这一点: // Waiting for the btnSave to be clicked $(document).on('click', '.btnSave', function
// 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:无法读取未定义的属性“设置”。您的代码
$(document).on('click', '.btnSave', function (event) {
....
if (isAdd == "Shipping") {
$('input[id="inputAddressName"]').rules("add", {
....
});
} else {
$('input[id="inputAddressName"]').rules("add", {
....
});
}
$('#customerAddressForm').validate(....); // <-initializes plugin
});
“有谁能告诉我它出了什么问题吗?”~没有。你必须告诉我们它出了什么问题。它在干什么?它没有做什么?控制台中出现了哪些错误?您还需要显示服务器端脚本,
isExistingAddressName
。请参阅remote
文档:我将重新表述我的问题,我想:是否有办法根据if/else语句在远程选项中传递url?例如:如果用户单击了“编辑”,则url应为“url编辑”。如果用户单击添加按钮,则url应为url添加。我将对我的代码进行注释以提供更多提示。顺便说一下,在控制台中,我只得到“无法读取未定义的属性‘设置’”。我尝试了您的解决方案,但它不起作用。我的代码有问题。我还试过:数据:{customerAddressId:function(){var isAdd=$('customerAddressForm').prop(“action”).split(“/”).pop();if(isAdd==“Shipping”){return$(“#customerAddressForm”).prop('action”).split(“/”.pop()}else{return return$(“-10”)}}
,但我得到了这个“=%5Bobject+Object%5D”附加到url。@adorablek,这不是我发布的任何消息。jQuery验证插件无法将任何内容附加到URL。我只是建议您通过从单击处理程序中删除.validate()
方法来正确初始化插件。您能看看我创建的jsbin吗:[.我按照你的建议做了,但是当它创建url时,我没有得到传递给它的值,但是customerAddressId=%5Bobject+Object%5D。我必须在浏览器中查看它,因为ajax请求在jsbin中不起作用。再次感谢你的帮助。@Adorablepolak,我不知道你在那个jsbin中试图向我显示什么,但我肯定认为你理解错了如何使用jQuery选择器…$(“-10”)
和$(“fdgdfgf”)
是毫无意义的胡言乱语。这就是为什么将对象对象
附加到GET
查询字符串中的原因。请参阅:即使jQuery选择器实际选择了某个对象,它仍然只是一个jQuery对象。您需要发送该对象的值,而不是整个对象。
$(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", {
....
});
}
});
});