我正在尝试将模板加载到主 .html 文件中的给定 DIV。我正在尝试使用 jquery:
$('#Selected').load("template.html",{REQUIREDID:PARENTID})
模板加载正确,但我无法传递变量并在模板中使用它。例如<div>"requiredid"</div>
如何读取加载模板时传递的参数
谢谢。
最佳答案
你应该这样做:
获取:
$('#Selected').load("template.html?requiredid=VALUE_TO_PASS");
发布:
$('#Selected').load("template.html",{requiredid=VALUE_TO_PASS});
在template.html
中添加以下javascript方法来读取查询参数:
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
并像这样使用它:
$('#someDivId').html(GetURLParameter('requiredid'));
希望对您有所帮助。
关于javascript - 传递参数 jquery.load,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132594/