开发者

Trigger asp.net link button click on enter key press

开发者 https://www.devze.com 2023-03-01 07:18 出处:网络
I have 2 asp text boxes that I have attached jQuery to trigger linkbutton server side click if enter key is pressed on any of the 2 text boxes. But this does not seem to work, Please help me explain w

I have 2 asp text boxes that I have attached jQuery to trigger linkbutton server side click if enter key is pressed on any of the 2 text boxes. But this does not seem to work, Please help me explain where and what I am doing wrong. I need a solution that works in all major browsers [IE7,8,9], [Firefox 3,4], Safari[4,5].

Here is my code,

开发者_高级运维<script language="javascript" type="text/javascript">
function GC_PostBack() {
    jQuery('#<%=lnkSubmitGiftCardNumber.ClientID%>').trigger("click");
}

and on server side pn Page_Load, I'm attaching this function to onkeypress event of textboxes.

 if (!IsPostBack)
    {
        txtGiftCardNumber.Attributes.Add("onkeypress", "if(window.event.keyCode == 13) { GC_PostBack(); }");
        txtGiftCardPin.Attributes.Add("onkeypress", "if(window.event.keyCode == 13) { GC_PostBack(); }");
    }

I have tried using .click() rather then .trigger("click"), but to no avail. Please help!

Thanks.


Alison is correct, and if you wanted to use jQuery like you have, just do:

<script language="javascript" type="text/javascript">
    function GC_PostBack() {
        jQuery('#<%=lnkSubmitGiftCardNumber.ClientID%>').click();
    }
</script>

EDIT :

If you are using jQuery, why not use jQuery for all of this? Currently, what you have will not work cross-browser for the keypress event. I suggest something like this:

(function() {

    $('input[id$="txtGiftCardNumber"], input[id$="txtGiftCardPin"]')
        .keypress(function(e) {

            var keyCode;

            if (window.event) keyCode = window.event.keyCode;
            else if(e) keyCode = e.which;
            else return true;

            if (keyCode == 13) {

                $('[id$="lnkSubmitGiftCardNumber"]').click();
                return false;

            } else return true;

        });

});


The following will work to trigger the postback:

document.getElementById('<%= lnkSubmitGiftCardNumber.ClientID %>').click();


You can find the correct answer here: http://johanleino.wordpress.com/2010/05/05/using-jquery-to-trigger-click-event-on-linkbutton/

The Problem is that the jQuery click method do not evoke the href of the linkbutton. You have to 'eval' the content of the 'href' attribute.


I understand that this is an old post, but there might be other people out there like me still searching around for the answer. I have tried this code and it works fine for me. Checking for "Enter key" is same but __doPostBack() instead of click().

__doPostBack('<%=lnkSubmitGiftCardNumber.UniqueID%>', '');


<asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click"runat="server">G</asp:LinkButton>

  $(document).keypress(function (e) {
        if (e.which == 13) {
            javascript: __doPostBack('LinkButton1', '')
        }
    });


You can make all in client side with JQUERY. let's say txtGiftCardNumber has txtGiftCardNumberID as ID in this case you can associate keydown that is a better event that keypress with IE

  $('input:text[id$=txtGiftCardNumberID ]').keydown(function (event) {
           if (event.keyCode == '13') {
            $('#'+<%= lnkSubmitGiftCardNumber.ClientID %>).click();
            }
        }
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消