I have the following input element on the page:
Search: <input id="example" />
I would like to capture the information the user enters in this box, when the user hits enter and display it on screen:
$(window).keypress(function(e) {
if(e.keyCode == 13) {
$("input[name=example]").val().insertAfter('#example');
}
The following results in an undefined:
$("input[name=example]").val() is undefined
How do you capture u开发者_JS百科ser inputed text?
You don't have a name
.
Use the #
selector: $('#example')
Try this...
$(window).keypress(function(e) {
if(e.keyCode == 13) {
$("#example").val().insertAfter('#example');
}
}
Since you is using id
and not name
, do:
$(window).keypress(function(e) {
if (e.keyCode == 13) {
$("input#example").val().insertAfter('#example');
}
});
精彩评论