如何使用jquery获取textfield的值并将其分配给php变量
How to get the value of textfield using jquery and assign it to php variable
我有一个简单的网页,它从输入框中获取值,并将其传递到下一个页面或表单。唯一的问题是我不知道如何使用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>
因此,整个文件将看起来像下面的
相关文章:
- 在运行时使用预先确定的对象分配变量
- 重新分配变量和重新定义之间的性能差异
- Javascript:在对象中分配变量时命名函数的好处
- Angularjs 控制器在服务方法返回之前从服务中分配变量
- 在 JavaScript 中的 for 循环中重新分配变量的目的是什么?
- 按设置的数量拆分字符串,并为拆分分配变量
- 嵌套的 if/else 语句讨论了分配变量.我已经这样做了,但它仍然不起作用
- 有没有理由不在 while 块声明中分配变量
- JSON - 访问 JSON 数组并分配变量
- Angular2 火力基地检索并分配变量
- 是否可以在运行依赖项之前在 gulp 任务中分配变量
- 在 JS 中的 for 循环中重新分配变量是什么意思
- 使用多种组合分配变量的最佳方法是什么
- 使用函数从字符串中分配变量名称
- 如何调用chrome.storage.sync.get并在页面加载时分配变量
- 在JavaScript中用冒号分配变量的语法有效吗
- 带有单选按钮的If语句不分配变量
- 正在从内部回调中分配变量
- Javascript在匿名函数中分配变量的值
- 使用jQuery分配变量类型时出错