我有一个简单的网页,它从输入框中获取值,并将其传递到下一个页面或表单。唯一的问题是我不知道如何使用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>";
?>
如何将结果值传递给"<a href='calculate.php?id={45}&tt='>
,其中tt
是框的值?
感谢
您应该使用一个函数来获取值,并更改按钮以使用该函数:
因此,您的最终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>
我删除了<php? print...
,因为它只是普通的html。
对于这样的场景,使用jquery非常简单。
<?php
print
"<input type='text' class='form-control' id='usr'>".
"<a id='button' href='#'><button class='btn btn-success'>Calculate</button></a>";
?>
在这里,我已经添加了id属性到你的锚标签或链接,无论你叫它什么。
现在jquery部分:
<script type="text/javascript">
$(document).ready(function(){
var usr = $("#usr").val();
$("#button").attr("href","calculate.php?id={45}&tt="+usr);
});
</script>
或者,如果您希望在特定文本字段的值更改时更改url,那么您可以尝试以下操作。
<script type="text/javascript">
$(document).ready(function(){
$("#usr").change(function(){
var usr = $("#usr").val();
$("#button").attr("href","calculate.php?id={45}&tt="+usr);
});
});
</script>
希望这能有所帮助。如果你想了解更多关于jquery变更事件的信息,那么你可以看看jquery变更活动
**********从这里编辑************
<!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>
因此,整个文件将看起来像下面的