I'm beginner at javascript so learning constantly very many new things so dont be angry if im asking a really stupid question because still learing the ropes. Trying to implement callback instead async: false. Adding this method to jquery Validator plugin. With async false works fine, but i don't wanna use it. Any help appreciated :)
我是javascript的初学者所以不断学习很多新东西,所以如果我问一个非常愚蠢的问题因为仍然学习绳索,不要生气。尝试实现回调而不是异步:false。将此方法添加到jquery Validator插件。使用async false工作正常,但我不想使用它。任何帮助赞赏:)
$.validator.addMethod( "CheckIfUsed",
function(value, element) {
var isSuccess
$.ajax({url :"/x/validate",
data : JSON.stringify({model:$(element).data('model'),
field:element.name,
equals:value}),
contentType : 'application/json',
type : 'POST',
async: true,
success: function(res){
{ isSuccess = res === "true" ? true : false }
}
})
return isSuccess
},"That data allready used");
Tryd this: Javascript callback functions with ajax - did not work
试过这个:用ajax的Javascript回调函数 - 没有用
Or am i doing something copletly wrong
或者我在做一些错误的事情
1 个解决方案
#1
0
From the documentation the remote method does almost exactly what you describe. https://jqueryvalidation.org/remote-method/ here is an example that I use in many parts of my current code-base.
从文档中,远程方法几乎完全符合您的描述。 https://jqueryvalidation.org/remote-method/这是我在当前代码库的许多部分中使用的示例。
$("#SIN").rules("add", {
remote: {
url: '/x/ValidateUsed',
async: true,
data: {
sin: function () { return $("#SIN").val(); },
clientId: $("#ClientID").val()
}
}
});
Just remember that "validate" or in my case "ValidateUsed" needs to return true
if not used or null
, undefined
, or a custom message like That data already used
if it is in use.
只记得“验证”或在我的情况下“ValidateUsed”需要返回true,如果没有使用或null,undefined或自定义消息,如该数据已被使用,如果它正在使用。
#1
0
From the documentation the remote method does almost exactly what you describe. https://jqueryvalidation.org/remote-method/ here is an example that I use in many parts of my current code-base.
从文档中,远程方法几乎完全符合您的描述。 https://jqueryvalidation.org/remote-method/这是我在当前代码库的许多部分中使用的示例。
$("#SIN").rules("add", {
remote: {
url: '/x/ValidateUsed',
async: true,
data: {
sin: function () { return $("#SIN").val(); },
clientId: $("#ClientID").val()
}
}
});
Just remember that "validate" or in my case "ValidateUsed" needs to return true
if not used or null
, undefined
, or a custom message like That data already used
if it is in use.
只记得“验证”或在我的情况下“ValidateUsed”需要返回true,如果没有使用或null,undefined或自定义消息,如该数据已被使用,如果它正在使用。