Javascript 如何重定向到另一个页面并从表中的 url 中传递参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14100563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:43:41  来源:igfitidea点击:

How to redirect on another page and pass parameter in url from table?

javascriptjquerytemplatesjquery-mobiletornado

提问by PaolaJ.

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

如何重定向到另一个页面并从表中的 url 中传递参数?我在 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=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 ?

我希望当我按下详细信息时重定向/player_detail?username=username并显示有关该播放器的所有详细信息。我尝试使用href="javascript:window.location.replace('./player_info');"内部输入标签,但不知道如何放入 result['username'] 。怎么做?

回答by pala?н

Set the user name as data-usernameattribute 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&& nullusing:

此外,您可以简单地检查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:

如果 value 不是,则评估为 true:

  • null
  • undefined
  • NaN
  • empty string ("")
  • 0
  • false
  • 空值
  • 不明确的
  • NaN
  • 空字符串 ("")
  • 0
  • 错误的

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

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

回答by Ravinder Singh

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');">

回答by Atticus

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;
});

回答by TennisVisuals

Here is a general solution that doesn't rely on JQuery. Simply modify the definition of window.location.

这是一个不依赖于 JQuery 的通用解决方案。只需修改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>