Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在HTML中传递url中的变量_Javascript_Html - Fatal编程技术网

Javascript 在HTML中传递url中的变量

Javascript 在HTML中传递url中的变量,javascript,html,Javascript,Html,我正在为我构建的应用程序使用HTML和Javascript。我正在使用onlclick功能,当我单击按钮时,它会将我带到新页面 函数编辑学生(学生ID){ console.log(studentId); document.location.href='/student/edit/{{studentId}'{ { 学生 } } } 学生证 学生角色 行动 {结果%中每个_学生的百分比} {{each_student.student_id} {{each_student.studnet_role

我正在为我构建的应用程序使用HTML和Javascript。我正在使用onlclick功能,当我单击按钮时,它会将我带到新页面

函数编辑学生(学生ID){
console.log(studentId);
document.location.href='/student/edit/{{studentId}'{
{
学生
}
}
}

学生证
学生角色
行动
{结果%中每个_学生的百分比}
{{each_student.student_id}
{{each_student.studnet_role}
{%endfor%}
当我点击“编辑”按钮时,它会转到带有此url的新页面, 我的预期输出是什么 /student/edit/studentid(必须打印studentid值,例如,如果值为1,则应为student/edit/1)

参考此答案

一些代码被删除,因为
{%result%}中的每个学生的
{%result%}
在这里不起作用

您正在将
studentId
传递给javascript,所以只需在javascript函数中使用该变量即可

函数编辑学生(学生ID){
console.log(studentId);
位置='/student/edit/'+studentId;
}

学生证
学生角色
行动
学生证
学生网络角色

如果进行串联,可能会起作用

document.location.href = '/student/edit/'+ studentId

在本例中,
studentId
作为Javascript变量传递给函数,但您使用
{{studentId}
将其视为仍在循环中。只需使用Javascript连接:

document.location.href = '/student/edit/' + studentId;

您正在传递id,因此需要连接该id而不是模板变量。因此,使用
+studentId
或javascript模板文字:
location=`/student/edit/${studentId}`