gpt4 book ai didi

javascript - 我怎样才能在点击输入时获得输入字段值

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:53:20 24 4
gpt4 key购买 nike

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

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>

最佳答案

使用 keyup()keydown()功能。附加到它,使用 event.keyCode == 13在点击 enter 后获得值(value)按钮。

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

nextPage.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");
}
}});

关于javascript - 我怎样才能在点击输入时获得输入字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36099981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com