【问题标题】:How to get which button is clicked?如何获取点击了哪个按钮?
【发布时间】:2014-06-21 06:38:40
【问题描述】:

我想在单击特定按钮时检查某些情况如何执行此操作?

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


     }

【问题讨论】:

    标签: jquery asp.net vb.net sender


    【解决方案1】:

    我通过将它分配给一个字符串值并将 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")
             }
    
         }
    

    【讨论】:

      【解决方案2】:

      如果您有很多按钮,请给它们一个相同的类名。例如: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”代替它。

      【讨论】:

        【解决方案3】:

        试试这个代码:

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

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

        【讨论】:

        • 我想要这个在 EndRequest 函数下不起作用。@kumar Manish
        • 怎么调用EndRequest函数,在函数下可以注册点击事件,像这样 $("#btnSubmit").click(function(){ //这里调用你的那个函数想打电话 });
        • 是的,但是当我在点击事件下发出警报时,它没有显示警报消息。@Kumar Manish
        • 如何调用EndRequest函数?
        猜你喜欢
        • 1970-01-01
        • 2011-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-08
        相关资源
        最近更新 更多