Javascript jQuery:检查是否单击了按钮
我在表单中有两个按钮,我想检查单击了哪个按钮。 单选按钮一切正常:Javascript jQuery:检查是否单击了按钮,javascript,jquery,html,Javascript,Jquery,Html,我在表单中有两个按钮,我想检查单击了哪个按钮。 单选按钮一切正常: if($("input[@name='class']:checked").val() == 'A') 在简单的提交按钮上,所有内容都会崩溃 谢谢 jQuery(':button').click(function () { if (this.id == 'button1') { alert('Button 1 was clicked'); } else if (this.id == 'bu
if($("input[@name='class']:checked").val() == 'A')
在简单的提交按钮上,所有内容都会崩溃
谢谢
jQuery(':button').click(function () {
if (this.id == 'button1') {
alert('Button 1 was clicked');
}
else if (this.id == 'button2') {
alert('Button 2 was clicked');
}
});
编辑:-这将适用于所有按钮。尝试以下操作:
$('#submit1, #submit2').click(function () {
if (this.id == 'submit1') {
alert('Submit 1 clicked');
}
else if (this.id == 'submit2') {
alert('Submit 2 clicked');
}
});
var focusout = false;
$("#Button1").click(function () {
if (focusout == true) {
focusout = false;
return;
}
else {
GetInfo();
}
});
$("#Text1").focusout(function () {
focusout = true;
GetInfo();
});
您应该对此稍作调整(例如,使用名称而不是id来提醒),但这样您就有了更通用的功能。您可以使用:
$("#id").click(function()
{
$(this).data('clicked', true);
});
现在通过if语句检查它:
if($("#id").data('clicked'))
{
// code here
}
有关详细信息,您可以访问发布您已经尝试过的代码。我的尝试:if($($(输入[@name='submit']:单击”).val()=='button')$(“输入[type='button'])。单击(函数(){alert(“id为+$(此).attr(“id”)+“已单击!”;});这是可行的,但对我不好,我有更多的按钮,而不仅仅是一个,所以我需要区分它。我尝试了很多代码,其中一些代码有问题,因为无法将其放入if语句中,因为不确定什么是返回true或1或nothing(尝试了所有操作)。我正在寻找单选按钮的最简单和类似的解决方案。@Kautil在您的else条件下输入错误
submit1
应该是submit2
if($("#id").data('clicked'))
{
// code here
}
$('#btn1, #btn2').click(function() {
let clickedButton = $(this).attr('id');
console.log(clickedButton);
});