LinkButton does not invoke on click()

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

Why doesn\'t this work?

    
    

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

    you need to give the linkButton a CssClass="myButton" then use this in the top

    $(document).ready(function() {
            $('.myButton').click(function(){
                 alert("hello thar");
            });
        });
    
    0 讨论(0)
  • 2020-12-29 08:40

    Do you want to submit the form, or add a Click event? Your link button translates to

    <a id="ttt" class="myButton" href="javascript:WebForm_DoPos[...]">Click</a>
    

    , so it has no on-click javascript. Therefore, .click(); does nothing.
    I haven't test it, but maybe this will work:

    eval($('.myButton').attr('href'));
    
    0 讨论(0)
  • 2020-12-29 08:41

    Just to clarify, only FireFox suffers from this issue. See http://www.devtoolshed.com/content/fix-firefox-click-event-issue. In FireFox, anchor (a) tags have no click() function to allow JavaScript code to directly simulate click events on them. They do allow you to map the click event of the anchor tag, just not to simulate it with the click() function.

    Fortunately, ASP.NET puts the JavaScript postback code into the href attribute, where you can get it and run eval on it. (Or just call window.location.href = document.GetElementById('LinkButton1').href;).

    Alternatively, you could just call __doPostBack('LinkButton1'); note that 'LinkButton1' should be replaced by the ClientID/UniqueID of the LinkButton to handle naming containers, e.g. UserControls, MasterPages, etc.

    Jordan Rieger

    0 讨论(0)
  • 2020-12-29 08:44

    The click event handler has to actually perform an action. Try this:

    $(function () {
        $('.myButton').click(function () { alert('Hello!'); });
    });
    
    0 讨论(0)
  • 2020-12-29 08:48

    trigger('click') fires jQuery's click event listener which .NET isn't hooked up to. You can just fire the javascript click event which will go to (or run in this case) what is in the href attribute:

     $('.myButton')[0].click();
    

    or

     ($('.myButton').length ? $('.myButton') : $('<a/>'))[0].click();
    

    If your not sure that the button is going to be present on the page.

    Joe

    0 讨论(0)
  • 2020-12-29 08:50

    If you need the linkbutton's OnClick server-side event to fire, you need to use __doPostback(eventTarget, eventArgument).

    ex:

    <asp:LinkButton ID="btnMyButton" runat="Server" OnClick="Button_Click" />
    
    <script type="text/javascript">
    
    function onMyClientClick(){ 
      //do some client side stuff
    
      //'click' the link button, form will post, Button_Click will fire on back-end
      //that's two underscores
      __doPostBack('<%=btnMyButton.UniqueID%>', ''); //the second parameter is required and superfluous, just use blank
    }
    
    </script>
    
    0 讨论(0)
提交回复
热议问题