开发者

How to Capture user inputted text using JQuery?

开发者 https://www.devze.com 2023-03-08 10:27 出处:网络
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 o

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');
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消