使用JQuery远程表单验证发送其他信息 [英] Sending additional information with JQuery remote form validation

查看:66
本文介绍了使用JQuery远程表单验证发送其他信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过查看两个不同的字段来远程验证一个字段.问题是,在我看到的有关如何进行远程验证发送其他数据的所有示例中,它们使用的是正在处理的其他字段的ID.因此,jQuery验证API页面上的remote()上的示例使用"#username",并将其发送到email字段之外.

I'm trying to remote validate a field by looking at two different fields. The issue is, in all the examples I see on how to do remote validate sending additional data, they're using the id of the other field that is being processed. So the example on the jQuery validate API page for remote() uses "#username" and sends that in addition to the email field.

我的页面很奇怪,并且具有多个相同的表格,并且页面上表格的数量是可变的,所以我不能为每个字段都拥有唯一的ID.有没有办法找出正在验证/进行远程呼叫的字段或表单?我尝试了以下类似的操作,因为我认为$(this)应该是正在验证的文本框,还是正在验证的单个表单,但事实并非如此.

My page is odd, and has multiple forms that are the same, and the number of forms on the page is variable, so I can't have a unique id for each field. Is there a way to find out which field or form is being validated/making the remote call? I tried something like the following, because I thought that $(this) would have been the text box being validated, or the individual form being validated, but that doesn't appear to be the case.

number: {
                  required: true,
                  number: true,
                  remote: {
                    url: "confirm.php",
                    data: {
                        toy: function() {
                            return $('select[name="toy"]:selected',this).val();
                        }
                    }
                  }
          }

谢谢, 杰瑞德(Jared)

Thanks, Jared

我最终想出一种使它工作的方法.传递表格的建议给了我一个主意.

I ended up figuring out a way to get it working. The advice to pass the form along gave me the idea.

由于页面上有多种表单,因此我一直在使用 $('form').each(function(){$(this).validate({....})});

Since I had multiple forms on the page, I was using $('form').each(function() {$(this).validate({....})});

因此,我只是在验证调用之前使用var form = $(this)保存了对当前表单的引用,然后在前面给出的示例中,我只需要进行一些小更改:

So I just saved a reference to the current form with var form = $(this) before the validate call, and then within the example I gave earlier I only had to make a small change:

data: {
    toy: function() {
        return $('select[name="toy"]',form).val();
    }
}

现在每个字段都知道它属于哪种形式!

And now each field knows which form it belongs to!

推荐答案

您可以为每个表单分配唯一的formId,并且每当提交表单时,都可以提交formId以及其他信息.表单以识别要提交的表单?

You could assign a unique formId to each of the forms, and whenever the form is submitted, you could submit the formId alongwith the other info from the form in order to identify which form is being submitted?

例如,在表单的html代码中,您可以执行以下操作:

For example, in the html code of your form, you could do something like this:

表格1 :(表格ID为1)

Form # 1: (with form id as 1)

<form id="form_1">
Name: <input type='text' id='name_1'> <br>
Address: <input type='text' id='address_1'> <br> 
<input type='button' value='Submit' onclick='validate(1);'>
</form>

表格2 :(表格ID为2)

Form # 2: (with form id as 2)

<form id="form_2">
Name: <input type='text' id='name_2'> <br>
Address: <input type='text' id='address_2'> <br> 
<input type='button' value='Submit' onclick='validate(2);'>
</form>

在javascript中,如下所示:

In the javascript, something like this:

function validate(formId)
{
   var data = {};
   //Example data that is to be submitted for validation:
   data.name = $("#name_" + formId).val();
   data.address = $("#address_" + formId).val();
   //.. and so on
   data.formId = formId;

   $.post('http://example.com/confirm.php', data, function(result)
     {
       //process the return of the remote validation here, 
       //found in the variable: result
     }
   );
}

这篇关于使用JQuery远程表单验证发送其他信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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