如何使用jQuery检测单击了哪个按钮
如何使用jQuery检测单击了哪个按钮如何使用jQuery检测单击了哪个按钮,jquery,html,Jquery,Html,如何使用jQuery检测单击了哪个按钮 <div id="dBlock"> <div id="dCalc"> <input id="firstNumber" type="text" maxlength="3" /> <input id="secondNumber" type="text" maxlength="3" /> <input id="btn1" type="button" value="Add" /> <
<div id="dBlock">
<div id="dCalc">
<input id="firstNumber" type="text" maxlength="3" />
<input id="secondNumber" type="text" maxlength="3" />
<input id="btn1" type="button" value="Add" />
<input id="btn2" type="button" value="Subtract" />
<input id="btn3" type="button" value="Multiply" />
<input id="btn4" type="button" value="Divide" />
</div>
</div>
注意:上面的“dCalc”块是动态添加的…由于该块是动态添加的,您可以尝试:
$("input").click(function(e){
var idClicked = e.target.id;
});
jQuery( document).delegate( "#dCalc input[type='button']", "click",
function(e){
var inputId = this.id;
console.log( inputId );
}
);
demojQuery可以绑定到单个输入/按钮,也可以绑定到表单中的所有按钮。单击按钮后,它将返回该按钮所单击的对象。从那里您可以检查属性,如值
$('#dCalc input[type="button"]').click(function(e) {
// 'this' Returns the button clicked:
// <input id="btn1" type="button" value="Add">
// You can bling this to get the jQuery object of the button clicked
// e.g.: $(this).attr('id'); to get the ID: #btn1
console.log(this);
// Returns the click event object of the button clicked.
console.log(e);
});
$('#dCalc输入[type=“button”]”)。单击(函数(e){
//“this”返回单击的按钮:
//
//您可以单击此按钮来获得按钮的jQuery对象
//例如:$(this).attr('id');要获取id:#btn1
console.log(this);
//返回所单击按钮的单击事件对象。
控制台日志(e);
});
从技术上讲,e
表示点击事件对象,而不是按钮对象。@mblase75,实际上点击事件对象是e.originalEvent
,e
是jQuery生成的自定义对象,为方便起见,具有“交叉浏览”属性,并且不包括e.originalEvent
中的某些内容:P@Esailija嗯,很明显我刚从技术上出去过。谢谢你提供的细节
$('#dCalc input[type="button"]').click(function(e) {
// 'this' Returns the button clicked:
// <input id="btn1" type="button" value="Add">
// You can bling this to get the jQuery object of the button clicked
// e.g.: $(this).attr('id'); to get the ID: #btn1
console.log(this);
// Returns the click event object of the button clicked.
console.log(e);
});