最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select submit button of specific form for click event - Stack Overflow

programmeradmin3浏览0评论

If I have the following form, how can I select the submit button based on the form ID to be used for a click event?

<form id="login">
    <input type="text" name="email">
    <input type="text" name="password">

    <input type="submit" name="submit">
</form>

Something like the following works, but it can't just be input[name=submit] because there may be more than one on the page.

$('input[name=submit]').click(function (e) {

        e.preventDefault();

        console.log('clicked');

    });

If I have the following form, how can I select the submit button based on the form ID to be used for a click event?

<form id="login">
    <input type="text" name="email">
    <input type="text" name="password">

    <input type="submit" name="submit">
</form>

Something like the following works, but it can't just be input[name=submit] because there may be more than one on the page.

$('input[name=submit]').click(function (e) {

        e.preventDefault();

        console.log('clicked');

    });
Share Improve this question asked Nov 16, 2012 at 0:19 MotiveMotive 3,1119 gold badges43 silver badges63 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

This will automatically select the form's submit control:

$('#login :submit').click(...);

http://api.jquery.com/submit-selector/

Cheers

Use :

 document.querySelectorAll("input[type=submit]")[0].click();

Use

$('#login input[type="submit"]')

You can read http://www.w3.org/TR/selectors/ for all CSS selectors.. (jQuery supports all default selector + more http://api.jquery.com/category/selectors/)

发布评论

评论列表(0)

  1. 暂无评论