gpt4 book ai didi

javascript - 如何在不使用提交按钮的情况下从 html 表单传递值

转载 作者:行者123 更新时间:2023-11-28 15:38:25 25 4
gpt4 key购买 nike

当前代码:索引.html

 <script type="text/javascript">            
$('#username').blur(function(){
$.ajax({
type: "POST",
url: "search.php",
data: {text:$(this).val()}
});
});
</script>

<form id="search" action="search.php" method="post">
<input type="text" id="username" name="username" size="30" placeholder="username ..." />
<input type="submit" id="submit-button" name="sa" value="search" />
</form>

搜索.php

<?php 
echo $_POST['username'];
?>

如果我按提交按钮,效果很好,但我想在焦点移出字段后发送字段中的值,而不按提交按钮。

最佳答案

您可以尝试focusout .

 $('#usernamecopy').focusout(function(){
$.ajax({
type: "POST",
url: "search.php",
data: {username :$('#username').val()}
});
});

在 php 文件中您可以使用:

$user= $_REQUEST['username'];

关于javascript - 如何在不使用提交按钮的情况下从 html 表单传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24839145/

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