【问题标题】:How to redirect on another page and pass parameter in url from table?如何在另一个页面上重定向并从表中传递 url 中的参数?
【发布时间】:2012-12-15 13:14:39
【问题描述】:

如何在另一个页面上重定向并从表中传递 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>  

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

【问题讨论】:

    标签: javascript jquery templates jquery-mobile tornado


    【解决方案1】:

    将用户名设置为按钮的data-username属性,同时也是一个类:

    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

    $(document).on('click', '.btn', function() {
    
        var name = $(this).data('username');        
        if (name) {
            window.location = '/player_detail?username=' + name;
        }
    });​
    

    正如answer中提到的那样

    if (name) {            
    }
    

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

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

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

    【讨论】:

    • 有一个小错误:函数之前的“(”不应该存在。
    • 对名称 var 的检查不应该是 &&,而不是 ||?
    • 一个问题,如果我想传递 3 个参数来构建另一个 url n img 我必须和你的答案一样吗?
    【解决方案2】:

    这是一个不依赖 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>
    

    【讨论】:

      【解决方案3】:

      这样做:

      【讨论】:

        【解决方案4】:

        绑定按钮,这是用jQuery完成的:

        $("#my-table input[type='button']").click(function(){
            var parameter = $(this).val();
            window.location = "http://yoursite.com/page?variable=" + parameter;
        });
        

        【讨论】:

          猜你喜欢
          • 2016-04-14
          • 2015-10-14
          • 2019-02-22
          • 1970-01-01
          • 1970-01-01
          • 2011-04-14
          • 2015-04-01
          • 2015-08-14
          • 2020-08-20
          相关资源
          最近更新 更多