代码之家  ›  专栏  ›  技术社区  ›  OvidiuPPP

用jquery获取跨度值

  •  0
  • OvidiuPPP  · 技术社区  · 6 年前

    好的,所以我正在使用SpringBoot开发一个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:每个th:在一个更高的分区中,如果我用th:文本设置文本,它会在我的页面上正确显示,因此值正在被传输)

    jquery案例:

    $(document).ready(function(){
    $('.glyphicon.glyphicon-remove').click(function(){
        var t = $(this).val();
        alert(t);
    });});
    

    但警报总是空的。我知道这个问题以前有人以某种形式提出过,但我找不到解决办法,我不知道我做错了什么。

    谢谢!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Ashok Kumar Sulung Nugroho    6 年前

    注意,只有输入有一个值。其他必须使用数据属性。

    <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);
        });});