如果页面上有多个表单,如何识别表单提交点击jQuery

时间:2011-10-13 21:55:13

标签: jquery

我有一个包含多个表单的页面。如果只有一个表格,我会做这样的事情:

$("input[type=submit]").click(function()

但是,如果有多个表单,并且随后在页面上有多个提交,jQuery如何识别点击来自哪里?

谢谢!

3 个答案:

答案 0 :(得分:2)

JQuery为每个匹配的元素分配一个click事件处理程序。函数内的this关键字是指“当前”提交按钮。

不要在提交中添加click事件处理程序,而应考虑使用表单元素的submit 事件。当用户在表单元素中按Enter键时,表单也将被提交,从而触发表单的onsubmit事件。

是否更改选择器以匹配表单取决于您。

您当前代码的概述:

$("input[type=submit]").click(function(){
    this.form; //Points to the current form
    this; //Points to the current element
    $(this); //JQuery object, representing the current element
});

答案 1 :(得分:1)

$('#form-id').bind('submit',function(){ });

一般是如何解决这个问题。

jQuery submit docs还有更多信息。

答案 2 :(得分:0)

试试这个

.. ..

$(“。test1 input [type = submit]”)。点击(function()

.. ..

$(“。test1 input [type = submit]”)。click(function(); $(“。test2 input [type = submit]”)。click(function()