我有一个存储在sql中的HTML模板,如下所示
<script id="Accounttmpl" type="text/x-jsrender">
<div class="authenticated" id="authenticated" >
<span class="loginWelcome">Hi </span>
<span class="loginWelcome">{{>FirstName}}</span>
<span id="Test" style="display:none">{{>(Verified)}} </span>
</div>
</script>
因此,在我的Javascript代码中,上面的模板被调用。但是,我想添加一条if语句,if verified == 'false'
该语句必须显示一条消息not verified
。
这是我的Javascript:
Success: function (result, context) {
if (result) {
$(context).html($('#Accounttmpl').render(result));
} else {
$(context).html($('#Accounttmpl').render({}));
}
}
因此,在我的结果中,我得到了“已验证”的详细信息,因此我尝试了以下操作:
Success: function (result, context) {
if (result) {
if(result.Verified === 'false'){
document.getElementById("Test").html("not verified")} //however it doesn't find the "Test" id and throws an error that it cant read property of null
$(context).html($('#Accounttmpl').render(result));
} else {
$(context).html($('#Accounttmpl').render({}));
}
}
}
因此,我的问题是如何检查#Accounttmpl
“ Test” ID,以便可以向其中添加if语句?
获得null的原因是因为您试图在将其添加到DOM之前获取Test的ID。
您在哪里:
if(result.Verified === 'false'){
document.getElementById("Test").html("not verified")
}
$(context).html($('#Accounttmpl').render(result));
更改订单回合:
$(context).html($('#Accounttmpl').render(result));
if(result.Verified === 'false'){
document.getElementById("Test").html("not verified")
}
另外,您在这里将JavaScript与jQuery混合使用:
document.getElementById("Test").html("not verified")
要么做:
document.getElementById("Test").textContent = "not verified";
要么
$("#Test").html("not verified")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句