LinkButton does not invoke on click()

前端 未结 8 904
不思量自难忘°
不思量自难忘° 2020-12-29 08:15

Why doesn\'t this work?

    
    

        
相关标签:
8条回答
  • 2020-12-29 08:53

    you need to assign an event handler to fire for when the click event is raised

        $(document).ready(function() {
            $('.myButton', '#form1')
                .click(function() {  
                    /* 
                       Your code to run when Click event is raised.
                       In this case, something like window.location = "http://..."                      
                       This can be an anonymous or named function
                    */
                    return false; // This is required as you have set a PostbackUrl
                                  // on the LinkButton which will post the form
                                  // to the specified URL
                }); 
        });
    

    I have tested the above with ASP.NET 3.5 and it works as expected.

    There is also the OnClientClick attribute on the Linkbutton, which specifies client side script to run when the click event is raised.

    Can I ask what you are trying to achieve?

    0 讨论(0)
  • 2020-12-29 09:00

    That's a tough one. As I understand it, you want to mimic the behavior of clicking the button in javascript code. The problem is that ASP.NET adds some fancy javascript code to the onclick handler.

    When manually firing an event in jQuery, only the event code added by jQuery will be executed, not the javascript in the onclick attribute or the href attribute. So the idea is to create a new event handler that will execute the original javascript defined in attributes.

    What I'm going to propose hasn't been tested, but I'll give it a shot:

    $(document).ready(function() {
    
    // redefine the event
    $(".myButton").click(function() {
       var href = $(this).attr("href");
    
       if (href.substr(0,10) == "javascript:") {
          new Function(href.substr(10)).call(this);
          // this will make sure that "this" is
          // correctly set when evaluating the javascript
          // code
       } else {
          window.location = href;
       }
    
       return false;
    });
    
    // this will fire the click:
    
    $(".myButton").click();
    
    });
    
    0 讨论(0)
提交回复
热议问题