如何使用 jQuery 远程验证依赖于表单中另一个字段的字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2710548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:06:30  来源:igfitidea点击:

How to use jQuery to remotely validate a field that depends on another field in the form?

jqueryvalidationformsjquery-validate

提问by Kevin Jhangiani

I have a form in which I am using remote validation to check if an email address already exists in the database. However, the catch is that on this form, the user can select between several different "groups", and each group has its own distinct set of email addresses (thus the same email can exist once in each group).

我有一个表单,我在其中使用远程验证来检查数据库中是否已存在电子邮件地址。然而,问题是在这个表单上,用户可以在几个不同的“组”之间进行选择,每个组都有自己不同的电子邮件地址集(因此同一电子邮件可以在每个组中存在一次)。

The group selection is a dropdown on the form, and the email address is an input field with remote validation. I have a couple issues. First, I have set up my remote rule like this:

组选择是表单上的下拉列表,电子邮件地址是带有远程验证的输入字段。我有几个问题。首先,我已经像这样设置了我的远程规则:

remote: {
    url: 'remote_script.php',
    data: {  group_id:  $('select.group_id').val() }
}

However, this seems to statically set the group_id parameter to whatever the first value in the select is. Meaning, if I change the select, then trigger the remote validation again, the group_id parameter does not change

但是,这似乎将 group_id 参数静态设置为 select 中的第一个值。意思是,如果我更改选择,然后再次触发远程验证,则 group_id 参数不会更改

First, how can I make this parameter dynamic, depending on the value of a select in the form?

首先,如何根据表单中选择的值使此参数动态化?

Secondly, how do I manually trigger the remote validation on the email address field? When the group_id select is changed, I want to re-trigger the remote validation on the email address field (without changing the value of the field). I tried using

其次,如何手动触发对电子邮件地址字段的远程验证?当 group_id 选择更改时,我想重新触发对电子邮件地址字段的远程验证(不更改该字段的值)。我尝试使用

$(selector).validate().element('.email_addr')

But this appears to only trigger the standard validation (required, email), and not the remote call.

但这似乎只触发标准验证(必需,电子邮件),而不是远程调用。

回答by Kevin Jhangiani

Found the solution to the second part of my question:

找到了我问题第二部分的解决方案:

 $(".email_addr").removeData("previousValue");

will remove the cache of the remote request, and allow the remote request to be triggered again, using .element().

将删除远程请求的缓存,并允许使用 .element() 再次触发远程请求。

Thus my code is as follows:

因此我的代码如下:

$("select.group_id").change(function() {
    $(".email_addr").removeData("previousValue"); //clear cache when changing group
    $("#customer_form").data('validator').element('.email_addr'); //retrigger remote call
    //my validator is stored in .data() on the form
});

Solution was found here: solution

解决方案在这里找到:解决方案

The first part of my question was answered originally by @Jeffery To

我的问题的第一部分最初由@Jeffery To 回答

All that needs to be done is to change the value of the parameter to a function, rather than just a value. Jeffery's example is copied below for future googlers:

所有需要做的就是将参数的值更改为一个函数,而不仅仅是一个值。Jeffery 的示例复制如下,供未来的 googlers 使用:

remote: {
  url: 'remote_script.php',
  data: {
    group_id: function () {
        return $('select.group_id').val();
    }
  }
}

回答by Jeffery To

From the second examplefor remote it looks like functions (evaluated during validation) can be used for data, so

从远程的第二个示例看来,函数(在验证期间评估)可用于数据,因此

remote: {
    url: 'remote_script.php',
    data: {
        group_id: function () {
            return $('select.group_id').val();
        }
    }
}

should work.

应该管用。

For your second question, did you try passing the validation rules to validate()?

对于您的第二个问题,您是否尝试将验证规则传递给validate()