Jquery 如何获取单击的按钮?
我想检查一些条件时,一个特定的按钮是点击如何做到这一点Jquery 如何获取单击的按钮?,jquery,asp.net,vb.net,sender,Jquery,Asp.net,Vb.net,Sender,我想检查一些条件时,一个特定的按钮是点击如何做到这一点 $(document).ready(function () { var prm = Sys.WebForms.PageRequestManager.getInstance(); prm.add_initializeRequest(InitializeRequest); prm.add_endRequest(EndRequest); Search("Other");
$(document).ready(function () {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
Search("Other");
});
function InitializeRequest(sender, args) {
}
function EndRequest(sender, args) {
alert(sender._postBackSettings.sourceElement.id)
var str1 = new String(sender._postBackSettings.sourceElement.id);
if (sender._postBackSettings.sourceElement.id == ContentPlaceHolder1_btnNew) {
alert("You have clicked new")
Search("NEW");
}
else {
alert("OTHERS")
Search("Other");
}
}
请尝试以下代码:
$(document).ready(function() {
$("#btnSubmit").click(function(){
// Call here that function which you want to call
});
});
阅读此链接:如果有很多按钮,请给它们一个相同的类名。例如:class=“myButton” 有关特定按钮的信息可以保存在其属性中。例如:objectId=“12345”那么您的代码可以如下所示:
$(".myButton").click(function(){
console.log($(this)); // this gives you the DOM Object of the button which is clicked
// now to get the attribute of the button i.e objectId you can do this
$(this).attr("objectId");
/* Depending on this you can handle your condition */
});
如果正在动态创建按钮,则可以使用
$(".myButton").live('click', function(){
console.log($(this)); // this gives you the DOM Object of the button which is clicked
// now to get the attribute of the button i.e objectId you can do this
$(this).attr("objectId");
/* Depending on this you can handle your condition */
});
对于Jquery 1.7.2以上的版本,不推荐使用live
您可以使用“on”来代替它。我通过将其分配给字符串值并将if条件作为字符串签入得到了解决方案
$(document).ready(function () {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
Search("Other");
});
function InitializeRequest(sender, args) {
}
function EndRequest(sender, args) {
var str1 = new String(sender._postBackSettings.sourceElement.id);
if (str1 == "ContentPlaceHolder1_btnNew") {
alert("You have clicked new")
}
else {
alert("You have clicked others")
}
}
我希望在EndRequest函数下使用此函数,但该函数不起作用。@kumar Manish如何调用EndRequest函数,在该函数下可以注册单击事件,如$(“#btnSubmit”)。单击(函数(){//在此处调用要调用的函数});是,但当我在click事件下发出警报时,它不会显示警报消息。@Kumar Manishhow如何调用EndRequest函数?