2014-05-05 220 views
2

我想检查一个特定的按钮被点击时的情况如何做到这一点?如何获得点击哪个按钮?

$(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"); 
      } 


    } 

回答

3

我得到了解决办法,将其分配给一个字符串值并检查条件是否为字符串。

$(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") 
     } 

    } 
+0

这正是我正在寻找的 - 谢谢! – Jeff

0

试试这个代码:

$(document).ready(function() { 

    $("#btnSubmit").click(function(){ 
     // Call here that function which you want to call 
    }); 
}); 

阅读此链接:http://api.jquery.com/click/

+0

我想这下,这是行不通的EndRequest功能。@库马尔马尼什 – sona

+0

你怎么调用EndRequest功能,该功能下,你可以注册单击事件,像这样$(“#btnSubmit按钮”)。点击(function(){ //在这里调用你想调用的函数 }); –

+0

是的,但是当我在click事件下发出警报时,它不显示警报消息。@ Kumar Manish – sona

0

如果你有充足的按钮,给他们一个相同的类名。例如: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。

您可以使用“on”代替它。