Jquery .VALIDATE:根据远程响应取消对字段的验证
考虑以下代码:Jquery .VALIDATE:根据远程响应取消对字段的验证,jquery,validation,jquery-validate,Jquery,Validation,Jquery Validate,考虑以下代码: $( document ).ready(function() { $("#register_employee_form").validate({ errorElement: "p", rules: { forename: "required", surname: "required", reportingto: { required : true,
$( document ).ready(function() {
$("#register_employee_form").validate({
errorElement: "p",
rules:
{
forename: "required",
surname: "required",
reportingto:
{
required : true,
remote: {
url: "ajax/error_checking/check_reporting_to.php",
type: "post",
data: {
reportingto: function() {
return $( "#reportingto" ).val(); // pass reportingto field
}
},
complete: function(response) {
if (response.responseText == "false")
{
// CANCEL THE VALIDATION FOR THIS FIELD
// HOW DO I DO IT??????
}
}
}
}
},
messages: {
forename: "Please enter your first name",
surname: "Please enter your last name",
reportingto: "Employee required"
},
submitHandler: function(form) {
form.submit();
}
});
});
如果AJAX调用返回的响应为false,我希望能够取消对字段“reportingto”的验证
感谢您的任何想法/帮助
问候
奥利
感谢到目前为止的回复,但我仍然有问题
我尝试使用Dependes,但仍然无法按要求工作:
采用以下“削减”测试代码
test_field:
{
required: {
remote: {
param: {
url: "ajax/error_checking/check_reporting_to.php",
type: "post",
data: {
reporting_to: function() {
return $( "#test_field_to" ).val(); // pass reporting_to field
}
},
complete: function(response) {
if (response.responseText == "false")
{
cancel_flag = 1;
}
}
} // param
,
depends: function() {
if(cancel_flag == 1)
{
return false;
}
else
{
return true;
}
}
} // remote
} // required
},
那场比赛呢
<td class="column_1"><label>TEST FIELD</label></td>
<td><input name="test_field" id="test_field" value="" /></td>
测试字段
如果我强制AJAX返回“false”,它仍然会触发字段的验证错误消息。使用depends参数
$( document ).ready(function() {
var cancelFlag=0; //Dont cancel validation
$("#register_employee_form").validate({
errorElement: "p",
rules:
{
forename:{required : true},//Do u assure on this LINE CODE BY U WORK ? I made it Proper , If ur Old code works then no problem
surname:{required : true},//Do u assure on this LINE CODE BY U WORK ? I made it Proper , If ur Old code works then no problem
reportingto:
{
required : true,
remote: {
param: {
url: "ajax/error_checking/check_reporting_to.php",
type: "post",
data: {
reportingto: function() {
return $( "#reportingto" ).val(); // pass reportingto field
}
},
complete: function(response) {
if (response.responseText == "false")
{
cancelFlag = 1;
// CANCEL THE VALIDATION FOR THIS FIELD
// HOW DO I DO IT??????
}
}
}//Param
,
depends: function() {
if(cancelFlag==1)
{
return false;
}
else
{
return true;
}
}
}//Remote
}
},
messages: {
forename: "Please enter your first name",
surname: "Please enter your last name",
reportingto: "Employee required"
},
submitHandler: function(form) {
form.submit();
}
});
});
我找到的解决方法是将标准remote:属性嵌套在名为param:的属性下。如果depends:函数返回true,它将引用您期望的属性
有关文档,请参阅
上面的代码应该可以工作,你也可以选择
reportingto:
{
required : {
remote: {
param: {
url: "ajax/error_checking/check_reporting_to.php",
type: "post",
data: {
reportingto: function() {
return $( "#reportingto" ).val(); // pass reportingto field
}
},
complete: function(response) {
if (response.responseText == "false")
{
cancelFlag = 1;
// CANCEL THE VALIDATION FOR THIS FIELD
// HOW DO I DO IT??????
}
}
}//Param
,
depends: function() {
if(cancelFlag==1)
{
return false;
}
else
{
return true;
}
}
}//Remote
}
}
用于报告元素 使用depends,
rules:{address:{required:{depends:function(element){if($('newsletterByPost').is('checked')){return true;}否则{return false;}}}}}}}
你能向你公司的高级PHP/javasript开发人员咨询一下吗?恐怕只有我一个人。我一个人工作。我离开几周了。我再看看。它应该起作用,所以我不明白它为什么不起作用。我会尝试一个新的例子,把所有的东西都剥离回来,以确保没有其他东西会影响它。谢谢你的友好回答,他们也有一点帮助,但我仍然无法让它按照我想要的方式工作。请参阅问题的补充:由于某种原因,上述解决方案似乎不会触发AJAX调用。奥利弗桑,你从上个月起就一直在努力解决这个问题吗?