原代码中使用each遍历自定义的字典集合,在onclick参数中传递字典集合的值,代码中js获取不到onclick中参数值 :
<span th:each="dict:${@dict.getType('dict_level')}" th:text="|${dict.dictLabel}|" th:value ="${dict.dictValue}" th:onclick="selectLevelSpan(this,${dict.dictValue})" />
<script>
function selectLevelSpan(level,levelValue) {
alert(levelValue);
}
</script>
解决方法:
在下面文章的评论里找到了解决方法,即onclick函数中的字典变量参数使用[[]]包裹:
https://ask.csdn.net/questions/702494
<span th:each="dict:${@dict.getType('dict_level')}" th:text="|${dict.dictLabel}|" th:value ="${dict.dictValue}" th:onclick="selectLevelSpan(this,[[${dict.dictValue}]])" />