从列表单击功能获取列表项的值

时间:2022-08-22 13:46:47

I need to get value form every list click

我需要从每个列表点击中获取价值

Here is my list:

这是我的清单:

<c:forEach items="${cmlist}" var="records"> 
   <ol class="dd-list"  id="chapterlist" >
      <li id="cs" class="dd-item" data-id="1" value="${records.levelID}">
         <div class="dd-handle">
            <a href="#subchaptercontent">Chapter:  ${records.levelName}</a> 
         </div>
      </li>
   </ol>
</c:foreach>

here is my click function code:

这是我的点击功能代码:

$(window).load(function(){
   $("#chapterlist li a").on("click", function(){
      level =  $(this).parent('li').val();
      console.log("level"+level);
   });
});

Could you please help?

能否请你帮忙?

2 个解决方案

#1


0  

parent() function only goes up one level of DOM, so it gets to your <div> element and stops there. Use parents() instead.

parent()函数只上升到一个级别的DOM,所以它到达你的

元素并停在那里。请改用parent()。

JSFiddle

Note, that val() will only work if value is an integer. Shouldn't be a problem with an ordered list, though, where value is the number of an item.

注意,val()只有在value是整数时才有效。但是,对于有序列表不应该是一个问题,其中value是项目的编号。

#2


0  

You cannot use the value method to get the value attribute of this object. Please, update your jsp by

您不能使用value方法来获取此对象的value属性。请更新您的jsp

 <c:forEach items="${cmlist}" var="records">    
                                <ol class="dd-list"  id="chapterlist" >
                                    <li id="cs" class="dd-item" data-id="1" data-value="${records.levelID}"><div class="dd-handle"><a href="#subchaptercontent">Chapter:  ${records.levelName}</a> </div>


   </li></ol></c:foreach>

And your javascript by

和你的javascript

level =  $(this).parent('li').data('value');

Fiddle to test this solution

小提琴来测试这个解决方案

#1


0  

parent() function only goes up one level of DOM, so it gets to your <div> element and stops there. Use parents() instead.

parent()函数只上升到一个级别的DOM,所以它到达你的

元素并停在那里。请改用parent()。

JSFiddle

Note, that val() will only work if value is an integer. Shouldn't be a problem with an ordered list, though, where value is the number of an item.

注意,val()只有在value是整数时才有效。但是,对于有序列表不应该是一个问题,其中value是项目的编号。

#2


0  

You cannot use the value method to get the value attribute of this object. Please, update your jsp by

您不能使用value方法来获取此对象的value属性。请更新您的jsp

 <c:forEach items="${cmlist}" var="records">    
                                <ol class="dd-list"  id="chapterlist" >
                                    <li id="cs" class="dd-item" data-id="1" data-value="${records.levelID}"><div class="dd-handle"><a href="#subchaptercontent">Chapter:  ${records.levelName}</a> </div>


   </li></ol></c:foreach>

And your javascript by

和你的javascript

level =  $(this).parent('li').data('value');

Fiddle to test this solution

小提琴来测试这个解决方案