本文介绍了如何重定向到另一个页面并从表中的 url 中传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重定向到另一个页面并从表中的 url 中传递参数?我在 tornato 模板中创建了这样的东西

How to redirect on another page and pass parameter in url from table ?I've created in tornato template something like this

<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>

我希望当我按下详细信息时重定向到 /player_detail?username=username并显示有关该玩家的所有详细信息.我尝试在 input 标签中使用 href="javascript:window.location.replace('./player_info');" 但不知道如何放入 result['username'] .如何做到这一点?

and I would like when I press detail to be redirect on /player_detail?username=usernameand show all detail about that player.I tried with href="javascript:window.location.replace('./player_info');" inside input tag but don't know how to put result['username'] in. How to do this ?

推荐答案

将用户名设置为 data-username 属性给按钮和一个类:

Set the user name as data-username attribute to the button and also a class:

HTML

<input type="button" name="theButton" value="Detail" class="btn" data-username="{{result['username']}}" />

JS

$(document).on('click', '.btn', function() {

    var name = $(this).data('username');
    if (name != undefined && name != null) {
        window.location = '/player_detail?username=' + name;
    }
});​

此外,您可以简单地检查 undefined &&null 使用:

Also, you can simply check for undefined && null using:

$(document).on('click', '.btn', function() {

    var name = $(this).data('username');
    if (name) {
        window.location = '/player_detail?username=' + name;
    }
});​

作为,在这个答案

if (name) {
}

如果值不是,将评估为真:

will evaluate to true if value is not:

  • 未定义
  • NaN
  • 空字符串 ("")
  • 0

上面的列表代表了 ECMA/Javascript 中所有可能的假值.

The above list represents all possible falsy values in ECMA/Javascript.

这篇关于如何重定向到另一个页面并从表中的 url 中传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:21
查看更多