我有一个包含多个表单的页面。如果只有一个表格,我会做这样的事情:
$("input[type=submit]").click(function()
但是,如果有多个表单,并且随后在页面上有多个提交,jQuery如何识别点击来自哪里?
谢谢!
答案 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)
答案 2 :(得分:0)
试试这个
.. ..$(“。test1 input [type = submit]”)。点击(function()
或
.. ..$(“。test1 input [type = submit]”)。click(function(); $(“。test2 input [type = submit]”)。click(function()