How can I get input field value on login

I am trying to get a value from an input field when the user presses enter. I do not need a button. How can i achieve this

test.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>
+4
source share
7 answers

Use the function keyup()or keydown(). When adding to it, use event.keyCode == 13to get the value after clicking the button 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>

User requirements

<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>

nextPage.php (create a new page. Make sure your page name matches the page name specified in <script></script>. Both are related to each other.)

<?php
$model = $_GET['model'];

//Now, use this '$model' wherever you want to use in this page. 

?>

Success and Failure

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

function getVal(ele) {
    if(event.keyCode == 13) {
        alert(ele.value);        
    }
}
<input type="text" onkeydown="getVal(this)"/>
+3
<script>
 $(document).on('keyup','#quan',function(){
 var code = e.keyCode || e.which;
  if(code == 13) { //Enter keycode
   //Do something
  }
 });
</script>
+3

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

    });
+3

:

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

-

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

where 13 enters the key code, when you enter it on the keyboard, you can see the value in the console

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

All Articles