繁体   English   中英

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

[英]How to pass value from html form without using submit button

当前代码:index.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>

search.php中

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

如果我按下提交按钮,则效果很好,但是当我聚焦于该字段后,我想从该字段发送值,而无需按提交按钮。

您可以尝试聚焦

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

在php文件中,您可以使用:

$user= $_REQUEST['username'];

未经测试,但我认为如果您将data: {text:$(this).val()}更改为data: {text:$("#usernamecopy").val()} 之所以有效,是因为将this放入data ; this是指data而不是#usernamecopy 顺便说一句,我希望您不要误以为#usernamecopy#username

**Use following code in JavaScript :**

     <script type="text/javascript"> 

            $('#username').blur(function(){
                var value = $("#username").val();
                $.ajax({
                    type: "POST",
                    url: "search.php",
                    data: "username="+value,
                    success: function(data){

                    }
                });                    
            });

     </script> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM