简体   繁体   English

JQuery 循环遍历动态元素并获取数据值

[英]JQuery loop through dynamic elements and get data-value

I am trying something to accomplish my requirement with collapsible panel我正在尝试使用可折叠面板来完成我的要求

 $(".sport").on("click", function() { var thisId = $(this).attr("id"); var thisChildren = $(this) + ".sportlist"; $(thisChildren).each(function(index) { }); });
 <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet" /> <div style="height:600px;padding:0px;margin:0px;"> <div class="col-md-3 scroll-container" style="padding:0px;"> <div class="panel-group" id="accordionMenu" role="tablist" aria-multiselectable="true"> <div class="panel panel-default"> <div class="panel-heading" role="tab"> <h4 class="panel-title"> <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse1" aria-expanded="true"> <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Cricket (2 items) </a> </h4> </div> <div class="panel-collapse collapse" id="collapse1" role="tabpanel" style="height: 0px;"> <div class="sportlist" data-value="1"> &nbsp;&nbsp; Sachin </div> <div class="sportlist" data-value="2"> &nbsp;&nbsp; Kohli </div> </div> <div class="panel-heading" role="tab"> <h4 class="panel-title"> <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse2" aria-expanded="true"> <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Other (2 items) </a> </h4> </div> <div class="panel-collapse collapse" id="collapse2" role="tabpanel" style="height: 0px;"> <div class="sportlist" data-value="3"> &nbsp;&nbsp; Bob </div> <div class="sportlist" data-value="4"> &nbsp;&nbsp; Willson </div> </div> </div> </div> </div> </div>

What I need is when I click on Cricket I need to store the data-value to an array.我需要的是,当我单击 Cricket 时,我需要将data-value存储到一个数组中。 I tried something but couldn't accomplish我尝试了一些东西,但无法完成

 $(".sport").on("click", function() { var thisId = $(this).attr("id"); var thisChildren = $(this) + ".sportlist"; $(thisChildren).each(function(index) { }); });

Use the href from a along with $.map :使用a中的href$.map

 $(".sport").on("click", function() { let id = $(this).attr("href"), list = $.map($(id).find(".sportlist"), function(item){ return $(item).data("value") }) console.log(list) });
 <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet" /> <div style="height:600px;padding:0px;margin:0px;"> <div class="col-md-3 scroll-container" style="padding:0px;"> <div class="panel-group" id="accordionMenu" role="tablist" aria-multiselectable="true"> <div class="panel panel-default"> <div class="panel-heading" role="tab"> <h4 class="panel-title"> <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse1" aria-expanded="true"> <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Cricket (2 items) </a> </h4> </div> <div class="panel-collapse collapse" id="collapse1" role="tabpanel" style="height: 0px;"> <div class="sportlist" data-value="1"> &nbsp;&nbsp; Sachin </div> <div class="sportlist" data-value="2"> &nbsp;&nbsp; Kohli </div> </div> <div class="panel-heading" role="tab"> <h4 class="panel-title"> <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse2" aria-expanded="true"> <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Other (2 items) </a> </h4> </div> <div class="panel-collapse collapse" id="collapse2" role="tabpanel" style="height: 0px;"> <div class="sportlist" data-value="3"> &nbsp;&nbsp; Bob </div> <div class="sportlist" data-value="4"> &nbsp;&nbsp; Willson </div> </div> </div> </div> </div> </div>

Here is another approach without using id这是另一种不使用 id 的方法

 $(".sport").click(function () { let ids = $.map($(this).closest(".panel-heading").next(".panel-collapse").find(".sportlist"), function (element) { return $(element).data("value") }); console.log(ids) });
 <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet" /> <div style="height:600px;padding:0px;margin:0px;"> <div class="col-md-3 scroll-container" style="padding:0px;"> <div class="panel-group" id="accordionMenu" role="tablist" aria-multiselectable="true"> <div class="panel panel-default"> <div class="panel-heading" role="tab"> <h4 class="panel-title"> <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse1" aria-expanded="true"> <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Cricket (2 items) </a> </h4> </div> <div class="panel-collapse collapse" id="collapse1" role="tabpanel" style="height: 0px;"> <div class="sportlist" data-value="1"> &nbsp;&nbsp; Sachin </div> <div class="sportlist" data-value="2"> &nbsp;&nbsp; Kohli </div> </div> <div class="panel-heading" role="tab"> <h4 class="panel-title"> <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse2" aria-expanded="true"> <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Other (2 items) </a> </h4> </div> <div class="panel-collapse collapse" id="collapse2" role="tabpanel" style="height: 0px;"> <div class="sportlist" data-value="3"> &nbsp;&nbsp; Bob </div> <div class="sportlist" data-value="4"> &nbsp;&nbsp; Willson </div> </div> </div> </div> </div> </div>

jQuery closes - For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. jQuery 关闭- 对于集合中的每个元素,通过测试元素本身并遍历其在 DOM 树中的祖先来获取与选择器匹配的第一个元素。

jQuery next - Get the immediately following sibling of each element in the set of matched elements. jQuery next - 获取匹配元素集中每个元素的紧随其后的兄弟。 If a selector is provided, it retrieves the next sibling only if it matches that selector.如果提供了选择器,则仅当它与该选择器匹配时,它才会检索下一个兄弟。

jQuery find - Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. jQuery 查找- 获取当前匹配元素集中每个元素的后代,由选择器过滤,jQuery object 或元素。

jQuery map - Pass each element in the current matched set through a function, producing a new jQuery object containing the return values. jQuery map - Pass each element in the current matched set through a function, producing a new jQuery object containing the return values.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM