thymeleaf将值传给onclick事件?下面介绍两种方法。
说明:文章中的categories参数是一个map数据结构。
目录
定义一个js函数
<script>
function list_search(cid) {
alert(cid)
}
</script>
方法一:
<div class="screen_con" id="cat_ids">
<a href="javascript:void(0)" th:onclick="|list_search('')|" class="screen_default red">
不限
</a>
<a href="javascript:void(0)" th:onclick="|list_search('${item.key}')|"
th:each="item:${categories}">
[[${item.value}]]
</a>
</div>
方法二:
<div class="screen_con" id="cat_ids">
<a data-id="" href="javascript:void(0)" onclick="list_search(this.getAttribute('data-id'))"
class="screen_default red">
不限
</a>
<a th:data-id="${item.key}" href="javascript:void(0)" onclick="list_search(this.getAttribute('data-id'))"
th:each="item:${categories}">
[[${item.value}]]
</a>
</div>