views:

144

answers:

2

I've got a .net button that has an href attribute value set to

javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cp1$ucInvoiceSearch$btnSearch", "", true, "", "", false, true))

I've got a textbox that when I press enter I want it to fire this event. Doing the 'Enter' event isn't an issue but I can't get the event on the href to fire using .click(). Here's my function so far:

$("[id*='tbInvNo']").keyup(function(event){
        var $btn = $(".pnl-invoice-search");
        if(event.keyCode == 13)
            $btn.click();
    });

I've got no idea how to get this to fire. Hope someone can help - jQuery and asp.net are driving me up the wall today! :(

A: 

With this code you can simulate the button click when the user clicks enter ANYWHERE, maybe it's better than your original plan. If not, you can adapt it to your needs :-)

    $(document).keypress(function (e) {   
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {   
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(
        "<%=yourBtn.ClientID.Replace('_','$') %>", '', true, '', '', false, true))
            return false;   
        } else {   
            return true;   
        }   
    }); 
Claudio Redi
jQuery will normalize keyCode for you.Edit: Looks like it normalizes to which.
lark
A: 

Instead of using href, use onclick="WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cp1$ucInvoiceSearch$btnSearch", "", true, "", "", false, true)) " and use trigger("click") instead of click().

lark
`trigger("click")` and `click()` just have the same effect...
Reigel