mpriney
mpriney

Reputation: 109

Redirect User On Enter Key Based on Text Field Value

I'm trying to simulate some software and I need the user to be able to enter some text into a field and then direct them to another page based on that text. I've created the code below but it doesn't seem to work. I'm using IE 7 and it just seems to refresh the page, it won't actually go to the new location. Anyone see why? Using jQuery 1.4.4 min.

Updated based on comments still not working

<form id="TEST"><input type="text" id="command" tabindex="1"></form>


<script type="text/javascript">
$(document).ready(function() {
$('#command').keypress(function(e) {
    if (e.which === 13) {
        if ($('#command').value === "AI") {
            window.location = "http://www.mozilla.org";
        }
        if ($('#command').value === "BRI") {
            window.location = "http://www.google.com";
        }
        }
    });
    });
</script>

Upvotes: 1

Views: 6094

Answers (2)

Cyclonecode
Cyclonecode

Reputation: 30071

The following is working:

<form method="post" id="TEST">
<input type="text" id="command" tabindex="1">
</form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"  type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('#command').keydown(function(e) {

    if (e.which === 13) {
      if ($('#command').val() === "AI") {
        window.location = "http://www.mozilla.org";
      }
      if ($('#command').val() === "BRI") {
        window.location = "http://www.google.com";
      }
      return false;
    }
  });
});
</script>

Upvotes: 5

mark1178
mark1178

Reputation: 47

<form method="post" id="TEST">
<input type="text" id="command" tabindex="1">
</form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"  type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#command').keydown(function(e) {

    if (e.which === 13) {
    if ($('#command').val() === "AI") {
        window.location = "http://www.mozilla.org";
    }
    if ($('#command').val() === "BRI") {
        window.location = "http://www.google.com";
    }
    return false;
    }
   });
   });
 </script>

This form works fine with the enter key being pressed. How would I get it to work with a Submit button rather than the enter key? I've changed .keydown to .click, .submit but neither works

Thank you

Upvotes: 1

Related Questions