Javascript 如何使用jquery获取textfield的值并将其分配给php变量

Javascript 如何使用jquery获取textfield的值并将其分配给php变量,javascript,php,jquery,Javascript,Php,Jquery,我有一个简单的网页,它从输入框中获取值并将其传递到下一个页面或表单。唯一的问题是我不知道如何使用jquery获取值并将其分配为php值。我想将此值传入URL 这是我的代码: <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="s

我有一个简单的网页,它从输入框中获取值并将其传递到下一个页面或表单。唯一的问题是我不知道如何使用
jquery
获取值并将其分配为
php
值。我想将此值传入
URL

这是我的代码:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

<?php
    print
        "<input type='text' class='form-control' id='usr'>".
        "<a href='calculate.php?id={45}&tt='><button class='btn btn-success'>Calculate</button></a>";

?>


应使用函数获取值,并更改按钮以使用该函数:

因此,您的最终php代码是:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>


<script>
    function calculate() {
        var tt = $("#usr").val();
        location.href = "calculate.php?id={45}&tt=" + tt;
    }
</script>

<input type='text' class='form-control' id='usr'>
<button class='btn btn-success' onclick="calculate()">Calculate</button>

函数计算(){
var tt=$(“#usr”).val();
location.href=“calculate.php?id={45}&tt=“+tt;
}
算计

我删除了
,使用jquery对于这种场景来说非常简单

<?php
print
    "<input type='text' class='form-control' id='usr'>".
    "<a id='button' href='#'><button class='btn btn-success'>Calculate</button></a>";

?>

这是不是从php的东西中消失了?对不起,请你把这个输入我的原始代码好吗?这使我更容易理解谢谢,但是如果它是一个php文件,你怎么做呢?我需要它在一个php文件,因为我打算添加更多的请,它真的会帮我一个忙lot@BenjMikesozery只需将代码粘贴到php文件中,而不使用,但它将不会是php文件谢谢,那么最终的文件会是什么样子-请将所有内容放在一起?很抱歉,我有一段时间没有在那里。请参见编辑的文本。我已经添加了最终文件。我希望这会有帮助。为了更好地理解,我建议查看jquery.com的参考链接。
<script type="text/javascript"> 
    $(document).ready(function(){
        var usr = $("#usr").val();
        $("#button").attr("href","calculate.php?id={45}&tt="+usr);
    });
</script>
<script type="text/javascript"> 
    $(document).ready(function(){
        $("#usr").change(function(){
            var usr = $("#usr").val();
            $("#button").attr("href","calculate.php?id={45}&tt="+usr);
        });            
    });
</script>
<!DOCTYPE html>
<html>
<head>
    <title>Whatever your title is</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body>
    <?php
    print
        "<input type='text' class='form-control' id='usr'>".
        "<a id='button' href='#'><button class='btn btn-success'>Calculate</button></a>";

    ?>

    <script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
    <script type="text/javascript"> 
        $(document).ready(function(){
            $("#usr").change(function(){
                var usr = $("#usr").val();
                $("#button").attr("href","calculate.php?id={45}&tt="+usr);
            });            
        });
    </script>
</body>
</html>