我有一个带有data-seat
anddata-row
属性的 div 元素:
<div class='selected' data-seat='1' data-row='1'></div>
<div class='selected' data-seat='2' data-row='1'></div>
<div class='selected' data-seat='3' data-row='1'></div>
<div class='selected' data-seat='1' data-row='2'></div>
<div class='selected' data-seat='2' data-row='2'></div>
我想为选定的座位打印友好消息:
var selectedPlaceTextFormated ='';
$(".selected").each(function () {
var selectedPlace = $(this);
selectedPlaceTextFormated += "Row " + selectedPlace.attr("data-row") + " (seat " + selectedPlace.attr("data-seat") + ")\n";
});
alert(selectedPlaceTextFormated);
此代码运行良好,并显示以下内容:
Row 1 (seat 1)
Row 1 (seat 2)
Row 1 (seat 3)
Row 2 (seat 1)
Row 2 (seat 2)
但是,我想要按行分组座位,即我想要以下内容:
Row 1(seats: 1,2,3)
Row 2(seats: 1,2)
另外,按行号排序。我怎样才能做到这一点?
谢谢。演示