jquery远程验证中if和else怎么写
How to write if and else in jquery remote validation
我需要在远程方法中添加一个if条件,因此对于没有$('#emailUrl').val()的表单,将陷入else条件
rules : {
lastName : {
required : true,
namevalidation : true
},
// Remote is the jquery function to check unique character using URL
primaryEmailId : {
required : true,
email : true,
remote : {
// If Condition required here. Please suggest me
url : $('#emailUrl').val(),
data: {
primaryEmailId: function() {
return $("#primaryEmailId").val();
},
}
},
}
}
你不能在url上放置条件,你需要定义函数然后需要放置条件。我不确定如果您通过空白 URL 进行远程验证会发生什么。请检查以下解决方案。
lastName : {
required : true,
namevalidation : true
},
// Remote is the jquery function to check unique character using URL
primaryEmailId : {
required : true,
email : true,
remote : {
// If Condition required here. Please suggest me
url : function (){
if (typeof ($('#emailUrl').val()) != 'undefined')
{
return $('#emailUrl').val();
} else {
return '';
}
},
data: {
primaryEmailId: function() {
return $("#primaryEmailId").val();
},
}
},
},
您需要做的是在远程规则中使用 depends
和 param
。 documentation 中对它们进行了一些描述,但尚不清楚它是否适用于远程(确实如此!)。
以您的示例为例,您将其重新设计为如下所示:
primaryEmailId : {
required : true,
email : true,
remote : {
//if this returns true, remote will be triggered
depends: function(){
return $('#emailUrl').val() != '';
},
//using these parameters
param: {
url : 'your validation url.php',
data: {
primaryEmailId: function() {
return $("#primaryEmailId").val();
},
}
}
},
}
我唯一不清楚的部分是为什么要根据用户调用不同的 URL?希望重要的部分更多是关于如何调用远程或不调用 :)
Working example(请注意,jsfiddle 对 ajax 调用有点奇怪,所以我的有点复杂)。
我需要在远程方法中添加一个if条件,因此对于没有$('#emailUrl').val()的表单,将陷入else条件
rules : {
lastName : {
required : true,
namevalidation : true
},
// Remote is the jquery function to check unique character using URL
primaryEmailId : {
required : true,
email : true,
remote : {
// If Condition required here. Please suggest me
url : $('#emailUrl').val(),
data: {
primaryEmailId: function() {
return $("#primaryEmailId").val();
},
}
},
}
}
你不能在url上放置条件,你需要定义函数然后需要放置条件。我不确定如果您通过空白 URL 进行远程验证会发生什么。请检查以下解决方案。
lastName : {
required : true,
namevalidation : true
},
// Remote is the jquery function to check unique character using URL
primaryEmailId : {
required : true,
email : true,
remote : {
// If Condition required here. Please suggest me
url : function (){
if (typeof ($('#emailUrl').val()) != 'undefined')
{
return $('#emailUrl').val();
} else {
return '';
}
},
data: {
primaryEmailId: function() {
return $("#primaryEmailId").val();
},
}
},
},
您需要做的是在远程规则中使用 depends
和 param
。 documentation 中对它们进行了一些描述,但尚不清楚它是否适用于远程(确实如此!)。
以您的示例为例,您将其重新设计为如下所示:
primaryEmailId : {
required : true,
email : true,
remote : {
//if this returns true, remote will be triggered
depends: function(){
return $('#emailUrl').val() != '';
},
//using these parameters
param: {
url : 'your validation url.php',
data: {
primaryEmailId: function() {
return $("#primaryEmailId").val();
},
}
}
},
}
我唯一不清楚的部分是为什么要根据用户调用不同的 URL?希望重要的部分更多是关于如何调用远程或不调用 :)
Working example(请注意,jsfiddle 对 ajax 调用有点奇怪,所以我的有点复杂)。