Javascript jquery php未将文本字段值放入url
我有一个简单的网页,它从输入框中获取值并将其传递到下一个页面或表单。唯一的问题是我不知道如何使用Javascript jquery php未将文本字段值放入url,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"
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>";
?>
$(文档).ready(函数(){
$(“#usr”).change(函数(){
var usr=$(“#usr”).val();
$(“#按钮”).attr(“href”,“calculate.php?id={45}&tt=“+usr”);
});
});
您在中缺少ID;
?>
如果您想在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>
$(文档).ready(函数(){
$(“#usr”).change(函数(){
var usr=$(“#usr”).val();
$(“#按钮”).attr(“href”,“calculate.php?id=&tt=“+usr”);
});
});
href
属性在按钮上不起任何作用,它们只能在
链接上使用。那里没有id为按钮的元素/标记。您在jquery中获得的是#按钮
,但它不存在,因此不会发生任何事情。此外,您通常不会用锚(
)来包装按钮我明白了,那么如何将它链接到按钮呢?理想情况下,我只希望它重定向到带有textfield值的calculate.php页面,对于带有method=“GET“
。它将获取表单字段,如usr
字段,并为您生成查询字符串。@JonathanKuhn对不起,这是讽刺吗?我是web开发新手,所以我不太清楚,我只是研究了一下。我稍微修改了一下您的代码,使之成为href='calculate.php?id={$values[“test”]}&tt='
。然后我把它放在jquery中,现在它不工作了——我怀疑这是因为$values[“test”]。如何将其包含在jquery中?是HTML还是php文件中的jquery。jquery不起作用。如果我手动为tt
添加一个值,比如说calculate.php?id={$values[“test”]}&tt=10
,它会工作,因为我在文件中进行了物理硬编码。如果我在文本字段中键入10并单击按钮,它仍然像calculate.php?id={$values[“test”]}&tt=
谢谢,但出于某种原因,它仍然没有显示值。这和按钮本身有关吗?因为如果我对按钮的href
进行硬编码,它会工作。您确定在脚本的这一部分正确设置了变量吗?
<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>