使用 JQuery 远程表单验证发送附加信息

发布于 2024-10-05 02:30:54 字数 1055 浏览 0 评论 0原文

我正在尝试通过查看两个不同的字段来远程验证字段。问题是,在我看到的有关如何远程验证发送附加数据的所有示例中,它们都使用正在处理的其他字段的 ID。因此,remote() 的 jQuery 验证 API 页面上的示例使用“#username”并将其发送到电子邮件字段。

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

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

谢谢, Jared

Edit:我最终找到了一种让它发挥作用的方法。传递表格的建议给了我这个想法。

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

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

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

现在每个字段都知道它属于哪个表单!

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.

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

Thanks,
Jared

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

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

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!

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

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

发布评论

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

评论(1

凉月流沐 2024-10-12 02:30:54

您可以为每个表单分配一个唯一的 formId,并且每当提交表单时,您都可以将 formId 与表单中的其他信息一起提交,以便识别哪个表单正在提交表格?

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

Form # 1: (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>

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 中,类似如下:

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

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?

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

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>

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>

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