25

我在一个表单中有两个按钮,想检查哪个按钮被点击了。使用单选按钮一切正常:

if($("input[@name='class']:checked").val() == 'A')

在简单的提交按钮上,一切都崩溃了。

谢谢!

4

6 回答 6

54
$('#submit1, #submit2').click(function () {
   if (this.id == 'submit1') {
      alert('Submit 1 clicked');
   }
   else if (this.id == 'submit2') {
      alert('Submit 2 clicked');
   }
});
于 2013-03-14T09:04:41.503 回答
22

你可以使用这个:

$("#id").click(function()
{
   $(this).data('clicked', true);
});

现在通过 if 语句检查它:

if($("#id").data('clicked'))
{
   // code here 
}

有关更多信息,您可以访问jQuery 网站上的 .data() 函数。

于 2015-10-17T18:56:02.130 回答
7
jQuery(':button').click(function () {
    if (this.id == 'button1') {
        alert('Button 1 was clicked');
    }
    else if (this.id == 'button2') {
        alert('Button 2 was clicked');
    }
});

编辑:- 这适用于所有按钮。

于 2013-03-14T09:03:03.010 回答
6
$('input[type="button"]').click(function (e) {
    if (e.target) {
        alert(e.target.id + ' clicked');
    }
});

你应该稍微调整一下(例如,使用名称而不是 id 来提醒),但是这样你就有了更通用的功能。

于 2013-03-14T12:55:14.813 回答
3
$('#btn1, #btn2').click(function() {
    let clickedButton = $(this).attr('id');
    console.log(clickedButton);
});
于 2020-07-22T10:27:32.973 回答
0

尝试类似:

var focusout = false;

$("#Button1").click(function () {
    if (focusout == true) {
        focusout = false;
        return;
    }
    else {
        GetInfo();
    }
});

$("#Text1").focusout(function () {
    focusout = true;
    GetInfo();
});
于 2013-03-14T09:15:55.437 回答