[英]How to redirect on another page and pass parameter in url from table?
How to redirect on another page and pass parameter in url from table?如何在另一个页面上重定向并从表中传递 url 中的参数? I've created in tornato template something like this
我在 tornato 模板中创建了这样的东西
<table data-role="table" id="my-table" data-mode="reflow">
<thead>
<tr>
<th>Username</th>
<th>Nation</th>
<th>Rank</th>
<th></th>
</tr>
</thead>
<tbody>
{% for result in players %}
<tr>
<td>{{result['username']}}</td>
<td>{{result['nation']}}</td>
<td>{{result['rank']}}</td>
<td><input type="button" name="theButton" value="Detail"
></td>
</tr>
</tbody>
{% end %}
</table>
and I would like when I press detail to be redirect on /player_detail?username=username
and show all detail about that player.我希望当我按下详细信息时重定向到
/player_detail?username=username
并显示有关该播放器的所有详细信息。 I tried with href="javascript:window.location.replace('./player_info');"
我试过
href="javascript:window.location.replace('./player_info');"
inside input tag but don't know how to put result['username'] in. How to do this?在 input 标签内,但不知道如何将 result['username'] 放入。如何做到这一点?
Set the user name as data-username
attribute to the button and also a class: 将用户名作为
data-username
属性设置为按钮以及类:
HTML HTML
<input type="button" name="theButton" value="Detail" class="btn" data-username="{{result['username']}}" />
JS JS
$(document).on('click', '.btn', function() {
var name = $(this).data('username');
if (name != undefined && name != null) {
window.location = '/player_detail?username=' + name;
}
});
EDIT: 编辑:
Also, you can simply check for undefined
&& null
using: 此外,您可以使用以下命令检查
undefined
&& null
:
$(document).on('click', '.btn', function() {
var name = $(this).data('username');
if (name) {
window.location = '/player_detail?username=' + name;
}
});
As, mentioned in this answer 正如在这个答案中提到的那样
if (name) {
}
will evaluate to true if value is not: 如果值不是,则评估为真:
The above list represents all possible falsy values in ECMA/Javascript. 以上列表表示ECMA / Javascript中所有可能的错误值。
Do this : 做这个 :
<script type="text/javascript"> function showDetails(username) { window.location = '/player_detail?username='+username; } </script> <input type="button" name="theButton" value="Detail" onclick="showDetails('username');">
Bind the button, this is done with jQuery: 绑定按钮,这是通过jQuery完成的:
$("#my-table input[type='button']").click(function(){
var parameter = $(this).val();
window.location = "http://yoursite.com/page?variable=" + parameter;
});
Here is a general solution that doesn't rely on JQuery. 这是一个不依赖于JQuery的通用解决方案。 Simply modify the definition of window.location.
只需修改window.location的定义即可。
<html>
<head>
<script>
function loadNewDoc(){
var loc = window.location;
window.location = loc.hostname + loc.port + loc.pathname + loc.search;
};
</script>
</head>
<body onLoad="loadNewDoc()">
</body>
</html>
HTML - set an id attribute HTML - 设置一个 id 属性
<input type="button" id="go-another-page" name="theButton" value="Detail">Go to another page with parameters</td>
JS - Create an action listener for redirecting JS - 创建一个用于重定向的动作监听器
const anotherPackage = document.getElementById('go-another-page');
anotherPackage.addEventListener('click', (event) => {
event.preventDefault();
// After ? mark set your key and variable eg: payment=order-consulting
// For multiple parameters you can use & eg: payment=order-consulting&amount=20
window.location.replace('/payment.html?payment=order-consulting');
});
Retrieve parameters from another page (In this case, payment.html)从另一个页面检索参数(在本例中为 payment.html)
// payment.js - this is javascript of your another page
document.addEventListener('DOMContentLoaded', (event) => {
const parameters = new URLSearchParams(window.location.search);
const payment = parameters.get('payment');
console.log(payment);
event.preventDefault();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.