jquery php没有将文本字段值放入url

时间:2016-04-06 23:19:44

标签: 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="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 type="text/javascript"> 
    $(document).ready(function(){
        $("#usr").change(function(){
            var usr = $("#usr").val();
            $("#button").attr("href","calculate.php?id={45}&tt="+usr);
        });            
    });
</script>

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

    ?>

单击按钮时,它无处可去,URL中没有显示任何值

我哪里错了?

由于

1 个答案:

答案 0 :(得分:0)

您在<a>元素中缺少要修改href的ID。

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

?>

如果要在jQuery代码中放置PHP变量,则需要从HTML中回退到PHP。

<script type="text/javascript"> 
$(document).ready(function(){
    $("#usr").change(function(){
        var usr = $("#usr").val();
        $("#button").attr("href","calculate.php?id=<?php echo $values['test']; ?>&tt="+usr);
    });            
});
</script>