【发布时间】:2018-06-12 06:18:36
【问题描述】:
我有一个名为“products”的数组变量(来自后端)。它具有以下结构:
--产品
-----特别
----------0
---------------身份证
---------------价格
---------------等.....
----------1
---------------身份证
---------------价格
---------------等
----------2..等
我正在使用百里香叶。我正在尝试在 javascript 中循环“特殊产品”并尝试使用它们的索引来获取子数据(id、价格等)。 我正在尝试使用一个名为“counter”的新整数变量来访问索引。所以我的代码是:
<script th:inline="javascript" th:with="setProducts=(${products.special})">
function LetsSayLoop() {
var counter = 0;
//looping through some checkboxes
$('#checkboxContainer .checkboxesClassName').each(function() {
if($(this).is(":checked")) {
//this is working:
var current_product_price = [[${setProducts[0].price}]];
//this is also working:
//var current_product_price = [[${setProducts[1].price}]];
//etc...
//this is not working:
var current_product_price = [[${setProducts[counter].price}]];
counter++;
}
});
}
</script>
我的 IDE (netbeans) 说变量“counter”没有被使用。生成的 Javascritp 和 HTML 也以以下结尾: 任何想法为什么?
var current_product_price =
编辑:关于 javascript 中循环的小补充:
//[[${#lists.size(setProducts)}]] outputs the desired integer;
for (i = 0; i < [[${#lists.size(setProducts)}]]; i++) {
//this is working:
console.log([[${setProducts[0].price}]]);
//this is not:
console.log([[${setProducts[i].price}]]);
}
编辑:(可能的答案?)
我知道 thymeleaf 无法识别 [[]] 范围内的本地 javascript 变量。例如:
var current_product_price = [[${setProducts[counter].price}]];
代码期望变量“counter”来自百里香。 我需要使用索引来做到这一点,但使用局部变量它不起作用。我可以增加一些本地 js 变量来完成这个吗?还是有其他方式?
【问题讨论】:
-
所以唯一的问题是您在 IDE 上看到的警告?代码工作正常吗?可能是 IDE 上的错误,他们有那些 :P 代码对我来说似乎很好。
-
不,代码不起作用。它中断并且输出以:“var current_product_price =”结束,就是这样。之后不再有代码
标签: javascript thymeleaf