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

发布于 2024-08-30 14:09:37 字数 610 浏览 1 评论 0原文

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

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

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

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

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

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

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

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-09-06 14:09:37

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

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

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

因此我的代码如下:

$("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
});

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

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

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

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

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().

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

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:

remote: {
  url: 'remote_script.php',
  data: {
    group_id: function () {
        return $('select.group_id').val();
    }
  }
}
莫相离 2024-09-06 14:09:37

从远程的第二个示例来看,它看起来像函数(在验证期间评估)可以用于数据,所以

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

应该可以工作。

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

From the second example for 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()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文