jQuery:检查按钮是否被点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15404890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Check if button is clicked
提问by hobok
I have two buttons in a form and want to check which one was clicked. Everything works fine with radioButtons:
我在一个表单中有两个按钮,想检查点击了哪个按钮。使用单选按钮一切正常:
if($("input[@name='class']:checked").val() == 'A')
On simple submit button everything crash.
在简单的提交按钮上,一切都崩溃了。
Thanks!
谢谢!
回答by Kautil
$('#submit1, #submit2').click(function () {
if (this.id == 'submit1') {
alert('Submit 1 clicked');
}
else if (this.id == 'submit2') {
alert('Submit 2 clicked');
}
});
回答by hobok
You can use this:
你可以使用这个:
$("#id").click(function()
{
$(this).data('clicked', true);
});
Now check it via an if statement:
现在通过 if 语句检查它:
if($("#id").data('clicked'))
{
// code here
}
For more information you can visit the jQuery website on the .data() function.
有关更多信息,您可以访问有关 .data() 函数的 jQuery 网站。
回答by Yoeri
$('input[type="button"]').click(function (e) {
if (e.target) {
alert(e.target.id + ' clicked');
}
});
you should tweak this a little (eg. use a name in stead of an id to alert), but this way you have more generic function.
你应该稍微调整一下(例如,使用名称而不是 id 来提醒),但这样你就有了更通用的功能。
回答by Pranav
jQuery(':button').click(function () {
if (this.id == 'button1') {
alert('Button 1 was clicked');
}
else if (this.id == 'button2') {
alert('Button 2 was clicked');
}
});
EDIT:- This will work for all buttons.
编辑:- 这适用于所有按钮。
回答by Francois Borgies
try something like :
尝试类似:
var focusout = false;
$("#Button1").click(function () {
if (focusout == true) {
focusout = false;
return;
}
else {
GetInfo();
}
});
$("#Text1").focusout(function () {
focusout = true;
GetInfo();
});