如何打印动态span类值?

时间:2017-11-11 06:28:24

标签: javascript jquery

我有一个动态的span类值。{{$school->name}}。问题是当我点击它时我可以看到警报的值。但不是每次都有。什么是最佳解决方案..任何人都可以建议吗?

  <div class="dropdown_wrapper">
       @foreach($schools as $school)
          <li class="school" id="school1">
              <span class="icon glyphicon glyphicon-education"></span>
              <span class="school_title">{{$school->name}}</span>
          </li>
       @endforeach
    </div>



 $(".school_title").on("click", function (argument) {
     var school = $(this).html();
        alert(school);
 });

1 个答案:

答案 0 :(得分:2)

如果我理解你的问题你正在寻找这种东西: -

 <div class="dropdown_wrapper">
   @foreach($schools as $key => $school)
      <li class="school" id="school1">
          <span class="icon glyphicon glyphicon-education"></span>
          <span id="{{$key}}" class="school_title">{{$school->name}}</span>
      </li>
   @endforeach
</div>



$(".school_title").on("click", function () {
   var getid = $(this).attr('id');
    var school = $("#"+getid).html();
    alert(school);
});

希望它有所帮助!