如何在按回车键时获取输入字段值

How can i get input field value on hitting enter

本文关键字:获取 输入 字段 回车      更新时间:2023-09-26

当用户按回车键时,我正在尝试从输入框中获取值。我不需要任何按钮。我怎样才能做到这一点

测试.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<form action="#" method="GET" enctype="multipart/form-data">
    <center>
            <input name="quan" align="middle" type="text" id="quan" placeholder="quan" title="If You Want Change Quantity Then Type Here"  size="4">
    </center>
</form>
<script type="text/javascript">
    $(document).ready(function(){
        var model=$('#quan').val();
        console.log(model);
    });                                     
</script>
</body>
</html>

使用keyup()keydown()函数。附加到它,使用 event.keyCode == 13 在点击按钮后获取enter值。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form action="#" method="GET" enctype="multipart/form-data">
    <center>
            <input name="quan" align="middle" type="text" id="quan" placeholder="quan" title="If You Want Change Quantity Then Type Here"  size="4">
    </center>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function() {
      $('#quan').keydown(function(event) {
          if (event.keyCode == 13) {
              var model=$('#quan').val();
              alert(model);
              // Use this 'model' variable
          }
      });
 });
</script>
</body>
</html>

用户要求

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function() {
      $('#quan').keydown(function(event) {
          if (event.keyCode == 13) {
              var model=$('#quan').val();
              $.ajax({url:"nextPage.php?model="+model,cache:false,success:function(result){
                    alert("success");
              }});
          }
      });
 });
</script>

下一页.php (创建一个新页面。确保您的页面名称与 <script></script> 中给出的页面名称匹配。两者都是相关的。

<?php
$model = $_GET['model'];
//Now, use this '$model' wherever you want to use in this page. 
?>

成功错误

$.ajax({url:"nextPage.php?model="+model,cache:false,success:function(result){
      if(result.status == 'success'){
                alert("success");
      } else if(result.status == 'error') {
                alert("Error");
      }
}});

function getVal(ele) {
    if(event.keyCode == 13) {
        alert(ele.value);        
    }
}
<input type="text" onkeydown="getVal(this)"/>

<script>
 $(document).on('keyup','#quan',function(){
 var code = e.keyCode || e.which;
  if(code == 13) { //Enter keycode
   //Do something
  }
 });
</script>

像这样

$(document).ready(function(){
     $('#quan').keyup(function (e){
            var model=$(this).val();
            if(e.keyCode == 13){
                alert(model)
              }
          })
    });

试试这个:

<script type="text/javascript">
$( "input" ).keypress(function( event ) {
  if ( event.which == 13 ) {
      alert($(this).val());
  }
});
</script>

你可以做这样的事情

<script type="text/javascript">
$( "input" ).keypress(function( event ) {
  if ( event.which == 13 ) {
      consol.log($(this).val());
  }
});
</script>

其中 13 是输入键代码 当您在键盘中输入时,您可以在控制台中看到该值

$(document).ready(function(){
     $('#quan').keyup(function (event) {
          if (event.which === 13) {
                var model = $(this).val();
                console.log(model);
          }
     });
});