好的,所以我正在使用Spring启动一个Web项目并使用Thymeleaf作为模板引擎,在一个页面中我有以下内容:
[...]<span th:switch="${cost.achitat}" class="pull-right">
<span th:case='true' class="btn btn-xs btn-default" >
<span class="glyphicon glyphicon-remove" aria-hidden="true" th:value="${cost.cost_id}"></span>
</span>
<span th:case='false' class="btn btn-xs btn-default">
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
</span>
</span>[...]
(对象的“achitat”参数是布尔值,我有一个th:每个都在一个更高的div中,如果我用th:text设置文本它在我的页面上正确显示,所以值正在传输)
我试图通过jquery获取 true 案例中的span值:
$(document).ready(function(){
$('.glyphicon.glyphicon-remove').click(function(){
var t = $(this).val();
alert(t);
});});
但警报始终为空。我知道之前已经以某种形式提出过这个问题,但是我找不到解决方案而且我不知道我做错了什么。
谢谢!
答案 0 :(得分:0)
请注意,只有输入才有值。其他人必须使用数据属性。
<span class="glyphicon glyphicon-remove" aria-hidden="true" th:attr="data-value=${cost.cost_id}"></span>
$(document).ready(function(){
$('.glyphicon.glyphicon-remove').click(function(){
var t = $(this).data('value'); // if int the use parseInt() function;
var t = paerseInt($(this).data('value'));
alert(t);
});});