How identify the form submit button clicked in a variable using Jquery

How can I assign the id of a submit button to a variable using JQuery?

I have the following HTML:

<form id="myForm">
    <input id="oknovo" type="submit" value="OK & Novo" />
    <input id="okfechar" type="submit" value="OK & Fechar" />
</form>

JS:

var botao;
$(document).ready(function () {

    $("#myForm input[type=submit]").click(function (event) {
        botao = $(this).attr('id');
        alert("id é " + botao);
    });
});

You may see the live JSFiddle here

I've already managed to indentify the clicked button wiht the help of this question but the problem now is to assign it to a global variable.


你的代码是正确的,你只需要添加结束括号)

$(document).ready(function () {
                 ^ unclosed

the problem now is to assign it to a global variable.

Either remove the line var botao; or take it out of the function you are calling onload (via the JSFiddle configuration on the left).


You are missing brackets on your fiddle. It should read:

var botao;
$(document).ready(function () {

    $("#myForm input[type=submit]").click(function (event) {
        botao = $(this).attr('id');
        alert("id é " + botao);
    });
});
链接地址: http://www.djcxy.com/p/95036.html

上一篇: 在倒数计时器结束时将变量设置为false

下一篇: 如何识别使用Jquery在变量中单击的表单提交按钮