jQuery Validator#remote与ASP.NET WebForms [英] jQuery Validator#remote with ASP.NET WebForms
问题描述
我正在尝试将jQuery Validator插件与WebForms一起使用.这是我的jQuery代码:
I'm trying to use the jQuery Validator plugin with WebForms. Here's my jQuery code:
$("input[id$=FromZip]").rules('add', {
required: true,
postalCode: true,
remote: {
url: "shipping companies.aspx/ValidatePostalCode",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: "{'postalCodeToValidate': '" + $("input[id$=FromZip]").val() + "'}",
dataFilter: function(data) { return (JSON.parse(data)).d; }
},
messages: {
required: '"From" zip code is required.',
postalCode: 'Invalid "From" zip code',
remote: 'The "From" zip code was not found in the database.'
}
});
此问题是val()
始终返回空,因此该值始终被标记为false.我尝试将其包装在函数调用中,如下所示:
The problem with this is that the val()
always returns empty, so the value is always being flagged as false. I tried wrapping it in a function call, like so:
remote: function() {
var r = {
url: "shipping companies.aspx/ValidatePostalCode",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: 'json',
async: false,
data: "{'postalCodeToValidate': '" + $("input[id$=FromZip]").val() + "'}",
dataFilter: function(data) { return (JSON.parse(data)).d; }
}
}
除了现在根本不调用WebMethod,也没有进行Ajax请求,而在进行调用之前(没有var r = {}
东西),但是传递的是空数据,而不是文本框中的值.我已经尝试了一天的大部分时间来解决这个问题.
Except now the WebMethod isn't being called at all and no Ajax request is being made, while before (without the var r = {}
stuff) the call was being made but passing empty data instead of the value from the textbox. I've been trying to figure this out for the better part of a day now.
有什么建议吗?
推荐答案
您可以这样编写远程函数
You can write your remote function like that
remote: function() {
var value = $('#FromZip').val();
var r = {
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ 'postalCodeToValidate': value }),
type: 'POST',
dataType: 'json',
url: 'shipping companies.aspx/ValidatePostalCode',
dataFilter: function(data) { return $.parseJSON(data).d; }
};
return r;
}
这篇关于jQuery Validator#remote与ASP.NET WebForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!