Javascript 无法在我的jquery模板中使用全局变量
在我的页面上有一个基于bootstrap的工件滑块,我定义了一个全局变量(在java脚本中)来计算从服务器返回的工件数量,用不同的类填充两个不同的div。但当我在jquery模板中使用变量时(我的if情况),我会得到下面的错误 TypeError:k.节点未定义 当我试图在console.log()中记录我的变量(“artifactCounterV”)时,在打印变量的值之后,我得到了这个错误 TypeError:无法将原始类型转换为原始类型 这是我的主页.jspJavascript 无法在我的jquery模板中使用全局变量,javascript,jquery,jquery-templates,Javascript,Jquery,Jquery Templates,在我的页面上有一个基于bootstrap的工件滑块,我定义了一个全局变量(在java脚本中)来计算从服务器返回的工件数量,用不同的类填充两个不同的div。但当我在jquery模板中使用变量时(我的if情况),我会得到下面的错误 TypeError:k.节点未定义 当我试图在console.log()中记录我的变量(“artifactCounterV”)时,在打印变量的值之后,我得到了这个错误 TypeError:无法将原始类型转换为原始类型 这是我的主页.jsp <html> <
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>home page</title>
<script language="javascript" type="text/javascript">
var restUrl = "<c:url value = '/rest/core/goods/products/book' />";
var restUrlOrderItem = "<c:url value = '/rest/order/orderItem/front/addToBasket/' />";
var artifactCounterV = 1;
$(function() {
fillTable();
});
function artifactCounter()
{
return artifactCounterV++;
}
function fillTable() {
$('#entityBody' + ' :not(script)').remove();
$.getJSON(restUrl + "/front/list/getAll", function(entities) {
if (entities.entityList)
$('#GridRowTemplate').tmpl(entities.entityList).prependTo(
'#entityBody');
else {
$('#GridRowTemplate').tmpl(entities).prependTo("#entityBody");
}
});
}
function addToShoppingCart(productId) {
$.ajax({
type : "POST",
url : restUrlOrderItem + productId,
success : function(res) {
if (res)
return res.id
}
});
}
function goToBasketPage(){
window.location="<c:url value ='/View/frontEnd/cart/Index.jsp'/>";
}
</script>
</head>
<body>
<span id="confirm"></span>
<%@ include file="Header.jsp"%>
<%@ include file="TopMenu.jsp"%>
<%@ include file="Commercial.jsp"%>
</body>
</html>
主页
var restUrl=“”;
var restUrlOrderItem=“”;
var=1;
$(函数(){
fillTable();
});
函数artifactCounter()
{
返回工件计数器v++;
}
函数fillTable(){
$(“#entityBody”+”:非(脚本)”.remove();
$.getJSON(restUrl+“/front/list/getAll”,函数(实体){
if(entities.entityList)
$('#GridRowTemplate').tmpl(entities.entityList).prependTo(
"实体",;
否则{
$('#GridRowTemplate').tmpl(entities).prependTo(“#entityBody”);
}
});
}
函数addToShoppingCart(productId){
$.ajax({
类型:“POST”,
url:restUrlOrderItem+productId,
成功:功能(res){
如果(res)
返回res.id
}
});
}
函数goToBasketPage(){
window.location=“”;
}
这是我的artifactSlider.jsp
jsp包含在commercial.jsp中,商业页面中没有java脚本代码,它是纯html
<!------------- product-slider ----------->
<div id="carousel-example" class="carousel slide hidden-xs "
data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div id="entityBody" class="entityBody">
<script id="GridRowTemplate" type="text/html">
${console.log(artifactCounterV)}
{{if artifactCounterV == 1 }}
<div class="item active">
<div class="row">
{{/if}}
{{if artifactCounterV==6}}
<div class="item">
<div class="row">
{{/if}}
<div class="col-sm-2 item-holder">
<div class="col-item">
<div class="photo">
<img
src="<c:url value='/rest/attachment/front/getFile/' />${pictureCode}"
class="img-responsive" />
</div>
<div class="info">
<div class="row">
<div class="price col-md-12">
<h5>${name}</h5>
<h5 class="price-text-color separator">
<span>price: </span> ${price} $
</h5>
</div>
</div>
<div class="separator">
<p class="btn btn-default col-md-12">
<i class="fa fa-shopping-cart"></i> <span href=""
class="hidden-sm" onclick="addToShoppingCart(${id})">add to basket</span>
</p>
<p class="btn btn-default col-md-12">
<i class="fa fa-list"></i> <a class="hidden-sm" href="">more</a> <span style="display: none">${artifactCounter()}</span>
</p>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
{{if artifactCounterV==6 || (artifactCounterV>6 && artifactCounterV<=12) }}
</div>
</div>
{{/if}}
</script>
</div>
</div>
</div>
<!------------- product-slider ----------->
${console.log(artifactCounterV)}
{{如果artifactCounterV==1}}
{{/if}
{{如果artifactCounterV==6}}
{{/if}
${pictureCode}”
class=“img响应”/>
${name}
价格:${price}$
加入篮子
${artifactCounter()}
{{如果artifactCounterV==6 | |(artifactCounterV>6&&artifactCounterV
我记得我尝试过类似的方法,问题是我必须在窗口或文档对象中创建变量。我记得我尝试过类似的方法,问题是我必须在窗口或文档对象中创建变量。您的JS代码是在DOM呈现之前还是之后包含的?这是我们可以调试的实时地方吗?亲爱的@Cruiser I编辑我的代码,就像它真的在我的sample@SergChernata不幸的是,你的JS代码是在DOM呈现之前还是之后包含的?这是我们可以调试的地方吗?亲爱的@Cruiser我编辑了我的代码,就像它真的在我的sample@SergChernata不幸的是没有