如何获得点击的按钮?

时间:2014-05-05 03:35:53

标签: 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");


     });

     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 个答案:

答案 0 :(得分:4)

我通过将其分配给字符串值并检查条件是否为字符串来获得解决方案。

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

     }

答案 1 :(得分:0)

试试这段代码:

$(document).ready(function() {

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

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

答案 2 :(得分: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以上的版本,不推荐使用

live。

您可以使用" on"而不是它。