How to respond to a specific keyword typed in a form input box using jQuery
I want to have a form with an input text field and be able to respond (display an alert text box) if the user types in the word 'hello' (case insensitive match).
What event do I need to trap (the text input field does not seem to have a change event.
This is what I have so far:
<html>
<head>
<title>Some test</title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<form action="somethin开发者_Go百科g.php" method="post">
Field1: <input id="field1" type="text">
</form>
<script type="text/javascript">
$(document).ready(function(){
// what?
});
</script>
</body>
</html>
Try:
$(document).ready(function(){
$('#Field1').keypress(function(){
if ($(this).val() === 'hello'){
alert('hello entered !!');
}
});
});
It does
<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
<link href="style.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#input1').change(function(){
if ($(this).val() == 'hello'){
alert('you typed hello !');
}
});
});
</script>
<style type="text/css"></style>
</head>
<body>
<input id="input1" type="text" />
</body>
</html>
I would use the keyup event coz it suits better to what you're intending to do. same codes already posted, just different event.
精彩评论