我有一个带有类名bus
的div在foreach循环php中重复
<div class="bus" id="result1">
<div class="seat red" data-number="A4(W)" data-book="1" amount="505" uniq-id="1452746805">
</div>
<div class="seat red" data-number="B4(W)" data-book="1" amount="505" uniq-id="1452746801">
</div>
<div class="seat red" data-number="C4(W)" data-book="1" amount="505" uniq-id="1452746823">
</div>
<div class="seat red" data-number="D4(W)" data-book="1" amount="505" uniq-id="1452746839">
</div>
</div>
<div class="bus" id="result2">
<div class="seat red" data-number="A4(W)" data-book="1" amount="501" uniq-id="1452746803">
</div>
<div class="seat red" data-number="B4(W)" data-book="1" amount="505" uniq-id="1452746802">
</div>
<div class="seat red" data-number="C4(W)" data-book="1" amount="505" uniq-id="1452746804">
</div>
<div class="seat red" data-number="D4(W)" data-book="1" amount="505" uniq-id="1452746801">
</div>
</div>
.....
.....
<div class="bus" id="resultn">
.....
....
</div>
点击带有课程seat
的div时,会向其添加课程green
我想用类green
计算每个div中的bus
个类。
这是我的代码
$(document).on("click",".seat",function(){
var total_class = $('.green').length;
})
问题是我从班级green
的所有div获得bus
班级总数
更新
当点击带有类座位的div时,我想将data-number属性值分别放入每个类总线的数组中,稍后它将用于某些数据库操作。
这是我的代码
$(document).on("click",".seat",function(){
var temp=[];
var seat_no = $(this).attr("data-number");
temp.push(seat_no);
}
问题是我从所有具有类总线的div中获取所有数据编号属性
答案 0 :(得分:1)
使用parent()方法访问您单击的元素上的父元素,这样您就可以获得类的实例数.green
var total_class = 0;
$(document).on("click",".seat",function(){
total_class = $(this).parent().children('.green').length
})
答案 1 :(得分:1)
试试这个:
在这种情况下
.bus
。.green
并获得其计数这就是我在这段代码中所做的:
var total_class = $(this).parent('.bus').children('.green').length;
<强>样本:强>
$(document).on("click", ".seat", function() {
$(this).addClass('green');
var total_class = $(this).parent('.bus').children('.green').length;
alert(total_class);
})
.red {
color: red
}
.green {
color: green
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bus" id="result1">
<div class="seat red" data-number="A4(W)" data-book="1" amount="505" uniq-id="1452746805">b1
</div>
<div class="seat red" data-number="B4(W)" data-book="1" amount="505" uniq-id="1452746801">b1
</div>
<div class="seat red" data-number="C4(W)" data-book="1" amount="505" uniq-id="1452746823">b1
</div>
<div class="seat red" data-number="D4(W)" data-book="1" amount="505" uniq-id="1452746839">b1
</div>
</div>
<hr>
<div class="bus" id="result2">
<div class="seat red" data-number="A4(W)" data-book="1" amount="501" uniq-id="1452746803">b2
</div>
<div class="seat red" data-number="B4(W)" data-book="1" amount="505" uniq-id="1452746802">b2
</div>
<div class="seat red" data-number="C4(W)" data-book="1" amount="505" uniq-id="1452746804">b2
</div>
<div class="seat red" data-number="D4(W)" data-book="1" amount="505" uniq-id="1452746801">b2
</div>
</div>
答案 2 :(得分:0)
$(document).on("click",".seat",function(){
var total_class = $(this).closest(".bus").find('.green').length;
})
这将为您提供在同一总线中具有绿色等级的总div:
答案 3 :(得分:0)
试试这个:
$(document).on("click",".seat",function(){
var total_class = $(this).parent().find('.green').length;
});
答案 4 :(得分:0)
$(document).ready(function () {
$('.seat').click(function () {
$(this).addClass('green');
// $('#total').val($('.bus').find('div.green').length);
szTotal = $('.bus').find('div.green').length;
szResult = "Total no of green class in current div (id=" + $(this).closest('div.bus').attr('id') + ") = " + $(this).closest('div.bus').find('div.green').length
szResult = szResult + "<br>" + "Total green class in All Div=" + szTotal
alert(szResult)
});
});
答案 5 :(得分:0)
$(document).ready(function(){
$('.seat').click(function(){
$(this).parent().addClass('green');
get_number_with_class('green');
});
});
function get_number_with_class(classname)
{
var total_class = $('div.'+classname).length;
}
对我有用