如果提交了任何表单,如何使用 jQuery 检查?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14969467/
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:09:19  来源:igfitidea点击:

How to check with jQuery if any form is submitted?

jqueryformsonsubmit

提问by Lozzano

I have a page with a lot of forms, and the user is going to pick something on that page. When the user does, I need to place that element somewhere else, much like a shopping cart really. But I can't seem to get more than the first form on the page to actually work as intended. I've tried multiple things:

我有一个包含很多表单的页面,用户将在该页面上选择一些内容。当用户这样做时,我需要将该元素放在其他地方,就像购物车一样。但我似乎无法让页面上的第一个表单真正按预期工作。我尝试了多种方法:

  1. Give all forms the same ID, which only caused the first form to act as I wanted, giving me the data of that form.
  2. Give them all the same ID again, this time appending a unique identifier to the ID, but of course, then my jQuery $('#something')doesn't catch it since it doesn't match.
  1. 为所有表单提供相同的 ID,这只会导致第一个表单按照我的意愿行事,为我提供该表单的数据。
  2. 再次给它们所有相同的 ID,这次将唯一标识符附加到 ID,但当然,然后我的 jQuery$('#something')没有捕获它,因为它不匹配。

So I am thinking how I would go about this. I need to only recieve the data from the submitted form on the page. And as mentioned, I could give them all a prefix of some sort, like I have now, but then I don't know how to match it.

所以我在想我会怎么做。我只需要从页面上提交的表单中接收数据。如前所述,我可以给他们所有的前缀,就像我现在一样,但后来我不知道如何匹配它。

Help would be greatly appriciated!

帮助将不胜感激!

Some code as requested (only an example to show what I mean, of course):

一些要求的代码(当然只是一个例子来说明我的意思):

The HTML:

HTML:

<form id="something-0"><input type="hidden" value="0"><input type="submit"></form>
<form id="something-1"><input type="hidden" value="1"><input type="submit"></form>

The jQuery:

jQuery:

$("#something-%").submit(function(e) {
e.preventDefault();
$("#some-span").html(data);
});

I want the #something-% to accept anything that comes after the "-", I hope you understand.

我希望 #something-% 接受“-”之后的任何内容,希望你理解。

回答by gilly3

As a general answer, to classify a group of elements, use the classattribute, giving each element the same class name. You can then select elements by that class. An element can have more than one class.

作为一般答案,要对一组元素进行分类,请使用class属性,为每个元素提供相同的类名。然后,您可以按该类选择元素。一个元素可以有多个类。

$(".something");

If you must use id, you can use an attribute selector:

如果必须使用 id,则可以使用属性选择器:

$("[id^='something']");  // all elements with an id beginning with 'something'
$("[id$='something']");  // all elements with an id ending with 'something'

For this specific question, since you want to act on any form within the page, the simplest choice would be to use the element name selector:

对于这个特定问题,由于您想对页面中的任何表单进行操作,最简单的选择是使用元素名称选择器:

$("form");

Regardless of the selector, you can identify which form was submitted by referencing thiswithin the submit()handler:

无论选择器如何,您都可以通过thissubmit()处理程序中引用来确定提交的是哪个表单:

$("form").submit(function (e) {
    e.preventDefault();
    var formId = this.id;  // "this" is a reference to the submitted form
});

回答by devrooms

You can catch all forms by using the selector 'form', e.g.

您可以使用选择器“表单”来捕获所有表单,例如

$("form").submit(function() {
  var theForm = $(this);
  var formID = theForm.attr("id");
  // do something
});

回答by Nilson Martins

Something like this...

像这样的东西...

   $('#myForm').submit(function (e) {
     e.preventDefault;
     if (e.result == true) {
       alert("This form was submitted!")
     }
   });