如何使用jQuery远程验证依赖于表单中另一个字段的字段? [英] How to use jQuery to remotely validate a field that depends on another field in the form?

查看:321
本文介绍了如何使用jQuery远程验证依赖于表单中另一个字段的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

b
$ b

组选择是表单上的下拉菜单,而电子邮件地址是具有远程验证的输入字段。我有几个问题。首先,我建立了如下的远程规则:

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

然而,这似乎静静地将group_id参数设置为select中的第一个值。这意味着,如果我改变了select,然后再次触发远程验证,那么group_id参数不会改变。首先,我怎样才能使这个参数成为动态的,取决于数值第二,如何在电子邮件地址栏中手动触发远程验证?当group_id选择更改时,我想重新触发电子邮件地址字段的远程验证(不更改字段的值)。我尝试使用

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

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

解决方案

发现问题的第二部分的解决方案:

  $(。email_addr)。removeData(previousValue); 

将删除远程请求的缓存,并允许使用重新触发远程请求。因此我的代码如下:

  $(select.group_id)。change(function(){
$(。email_addr)。removeData(previousValue); //更改组时清除缓存
$(#customer_form ).data('validator')。element('。email_addr'); //重新触发远程调用
//我的验证器以.bata()形式存储在
中)

解决方法在此处找到:解决方案



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



所有需要做的事情是将参数的值改为一个函数,而不仅仅是一个值。 Jeffery的例子在下面被复制到未来的googlers:

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


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.

解决方案

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

这篇关于如何使用jQuery远程验证依赖于表单中另一个字段的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆