I want to save a series of array data. When I trigger the click () event, I will store this data in the array, but only one array can be created, so every time click is triggered, I store the data in the same array. I want to distinguish different data in different arrays, but I cannot create them manually, because the data is generated dynamically. I"m not sure how many variables I need. This is probably the case.
as shown in the following figure
,
:
,
,,
[{floor: 1, roomNos: ["102", "103"]}, {floor: 2, roomNos: ["204"]}]
current code:
<div id="son_house_room_number_btns">
1
<div class="weui-flex">
<div class="weui-flex__item">
<div class="placeholder">
<a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">101</a>
</div>
</div>
<div class="weui-flex__item">
<div class="placeholder">
<a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">102</a>
</div>
</div>
<div class="weui-flex__item">
<div class="placeholder">
<a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">103</a>
</div>
</div>
</div>
2
<div class="weui-flex">
<div class="weui-flex__item">
<div class="placeholder">
<a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">201</a>
</div>
</div>
<div class="weui-flex__item">
<div class="placeholder">
<a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">202</a>
</div>
</div>
<div class="weui-flex__item">
<div class="placeholder">
<a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">203</a>
</div>
</div>
<div class="weui-flex__item">
<div class="placeholder">
<a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_default">204</a>
</div>
</div>
</div>
</div>
selectRoomNoList = [];
arr=[]
$("-sharpson_house_room_number_btns>.son_house_room_number_area_div>.weui-flex>.weui-flex__item>.placeholder>.weui-btn").click(function() {
//
var btn_first = $(this).attr("class");
//
var btn_zdy=$(this).attr("data-index");
// console.log(btn_zdy)
if(btn_first == "weui-btn weui-btn_disabled weui-btn_default") {
$(this).attr("class", "weui-btn weui-btn_plain-primary");
//
var floor_p = $(this).parent().parent().parent().siblings("p").children("a");
// selectRoomNoList.push($(this).text());
arr.push($(this).text());
selectRoomNoList.push({floorNo:floor_p.text(),rooms:arr});
console.log(selectRoomNoList)
} else {
$(this).attr("class", "weui-btn weui-btn_disabled weui-btn_default");
// , null
// indexOf:
selectRoomNoList[selectRoomNoList.indexOf($(this).text())] = null;
// splice null
// splice(:,/, :,)
selectRoomNoList.splice(selectRoomNoList.indexOf(null), 1);
console.log(selectRoomNoList)
}
});
many methods have been tried but have not been solved, or is there a better way to solve this problem?