开发者

How to recognize a form submit click in jQuery if there is more than one form on a page

I have a page with more than one form. If there was just one form, I'd do something like this:

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

But how does jQuery recognize where the click came from if there is mor开发者_运维问答e than one form, and subsequently more than one submit on the page?

Thanks!


JQuery assigns a click event handler to each matching element. The this keyword inside the function refers to the "current" submit button.

Instead of adding a click event handler to the submit, consider using the submit event of the form element. When the user presses enter from within a form element, the form will also be submitted, triggering the onsubmit event of the form.

It's up to you whether you change the selector to match the form or not.

An overview for your current code:

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


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

Is generally how you solve this problem.

jQuery submit docs has further info.


Try this

.. ..

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

or

.. ..

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜