猿问

将变量传递给 html javascript 中的 onclick 方法

将变量传递给 onclick 方法。如何在 HTML 和 javascript 中执行此操作


我将变量 userid 从 onclick 调用函数传递给被调用函数。


<tbody>

                {% for each_user in detail %}


                <tr>

                    <td>{{ each_user.userss_name }}</td>

                    <td>{{ each_user.userss_email }}</td>     


                    <td><input class="btn btn-info" type="button" value="Edit" id="edit"

                            onclick="editUser('\''+ '{{ each_user.userss_id }}' +'\'')"></td>

                </tr>

                {% endfor %}

            </tbody>

我的脚本代码是


        <script>

            function edituser(user_id) {

                console.log(user_id);

                document.location.href = "/'{{ user_id }}'/user/edit/"

            }

        </script>

预期的结果是当我单击编辑按钮时,它应该将我带到 url /user_id/user/edit。所以用户ID是这里的整数。所以我应该看到/1/user/edit。但我在这里得到的是 http:/""/interviewer/edit 和 page not found 错误。所以我不知道如何将变量传递给 url。


宝慕林4294392
浏览 315回答 2
2回答

紫衣仙女

在document.location.href下面编辑你喜欢的应该有效。问题是 user_id一个 javascript 变量,而不是一个模板变量,所以你不要用 {{ }} 包装它。&nbsp;<script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function edituser(user_id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(user_id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.location.href = "/" + user_id + "/user/edit/"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</script>

撒科打诨

&nbsp; &nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function edituser(user_id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(user_id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.location.href = "/" + user_id + "/user/edit/"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </script>user_id 不需要在 {{}} 中
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答