2

Im trying to redirect to another page when user hits return.But its not working:

$(function() {
    $("#ctl00_ContentPlaceHolder1_txtPopCEP").keypress(function(e) {
        var tecla = (e.which) ? e.which : e.keycode;

        if (tecla == 13) {
            e.preventDefault();
            window.location.href = "http://www.chinainbox.com.br/site2010/lojaBuscaResultado.aspx?cep=" + $(this).text() + "&pop=yes";
            // __doPostBack('ctl00$ContentPlaceHolder1$ImageButton1', '');
        }
    });
});

Any ideias?

ozsenegal
  • 4,055
  • 12
  • 49
  • 64

4 Answers4

1

update: as pointed out by Stefan Kendall in the comments, the following only applies to jQuery versions < 1.4.3


Looks like it should work -- have you tested to ensure the event is firing? Keypress is supposedly not very reliable, you should try switching to keydown or keyup

See the question jquery kepress event not firing

Community
  • 1
  • 1
Daniel Mendel
  • 9,862
  • 1
  • 24
  • 37
0

try doing window.location instead of window.location.href.

stevebot
  • 23,275
  • 29
  • 119
  • 181
0

This line...

var tecla = (e.which) ? e.which : e.keycode;

Is not needed. jQuery takes care of this cross-browser issue. Also, the "c" in keycode must be captialized, and that is all you need to fix...

var tecla = e.keyCode;
Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
  • @user257234 I wouldn't have taken the time to answer if I was not sure – Josh Stodola Oct 21 '10 at 20:29
  • @user257234 You might want to try `keyup` instead of `keypress`. In the jQuery docs (http://api.jquery.com/keypress/) they mention issues with using keypress in combination with keycode. – Josh Stodola Oct 21 '10 at 20:36
  • @Stefan What part does not work in 1.3.2? I thought the handling of e.which goes way back. – Josh Stodola Oct 21 '10 at 21:36
  • Nope. Go read through the bug reports. Enter, tab, delete, and some other characters have issues and don't work right. – Stefan Kendall Oct 21 '10 at 21:41
  • @Stefan Thanks for the link... I think those issues are related to the keypress event, which I already mentioned in a previous comment. – Josh Stodola Oct 21 '10 at 21:43
  • I don't think this is explicitly on keypress, but I wouldn't compromise design to fix bugs. I'd say "upgrade to 1.4.2" is a better suggestion, but you should at least mention the caveat. Not everyone can run 1.4.2 due to existing environment constraints. – Stefan Kendall Oct 21 '10 at 22:17
  • No, it doesn't. I guess the inconsistency is in FF for keycode. http://api.jquery.com/keypress/ Read the comments. `e.which` is supposed to be normalized. – Stefan Kendall Oct 22 '10 at 00:17
-1

I think you should be using window.location= instead of window.location.href=

FatherStorm
  • 7,133
  • 1
  • 21
  • 27