请帮助我如何: 如果检查了复选框父项,则检查所有兄弟姐妹,如果选中了复选框的某些子/子项,则还必须检查父项。我怎么能这样做?
我的laravel代码中有这个:
<table class="table table-hover" id="tableOrders">
<thead class="thead-inverse">
<tr>
<th width="4%"><input type="checkbox" id="selectAll" /></th>
<th width="7%">USER ID</th>
<th width="12%">AMOUNT</th>
<th width="15%">SHIP DATE</th>
<th width="15%">RECEIVER</th>
<th width="20%">ADDRESS</th>
<th width="14%">MOBILE #</th>
</tr>
</thead>
<tbody>
<?php $i=0 ?>
@foreach($shippings as $data)
<tr>
<td><input type="checkbox" class="parent" name="shippings[][]" value="{{ $data->shipping_id }}" /> </td>
<td><a href="#" data-value="{{ $data->user_id }}" class="linkedit"> {{ $data->user_id }} </a></td>
<td><a href="#" data-value="{{ $data->total_amount }}" class="linkedit"> {{ $data->total_amount }} </a></td>
<td><a href="#" data-value="{{ $data->shipping_date }}" class="linkedit"> {{ $data->shipping_date }} </a></td>
<td><a href="#" data-value="{{ $data->shipping_receiver }}" class="linkedit"> {{ $data->shipping_receiver }} </a></td>
<td><a href="#" data-value="{{ $data->shipping_address }}" class="linkedit"> {{ $data->shipping_address }} </a></td>
<td><a href="#" data-value="{{ $data->mobile_number }}" class="linkedit"> {{ $data->mobile_number }} </a></td>
</tr>
<tr>
<td></td>
<td></td>
<td><p style="font-weight:bold;">Product Name</p></td>
<td><p style="font-weight:bold;">Price</p></td>
<td><p style="font-weight:bold;">Ordered Qty</p></td>
<td><p style="color:red;font-weight: bold;">Available Qty</p></td>
</tr>
@foreach($data->products as $product)
<tr>
<td></td>
<td><input type="checkbox" class="child" name="shippings[{{$i}}][products][]" value="{{ $product->product_id }}" /> </td>
<td>{{ $product->product_name }}</td>
<td>{{ $product->price }}</td>
<td>{{ $product->quantity }}</td>
<td><p style="color:red;"> {{ $product->available_qty }}</p></td>
</tr>
@endforeach
<?php $i++; ?>
@endforeach
</tbody>
</table>
我有我的jsfiddle链接:
http://jsfiddle.net/jKxNF/112/
我很困惑,因为我在我的laravel循环中完成了它,但它的结构是在jsfiddle中。
答案 0 :(得分:1)
我会在触发复选框中添加一个属性,例如
<input type="checkbox" data-target=".prefix-{{ $data->shipping_id }}">
并在目标<td>
<td class="prefix-{{ $data->shipping_id }}"><input type="checkbox">Sample3</td>
以下Javascript将选择相关的复选框
$(document).on('change', 'input[type="checkbox"]', function(e) {
var checked = $(this).prop('checked')
var target = $(this).data('target')
if(checked){
$(target).find('input').prop('checked', true)
} else {
$(target).find('input').prop('checked', false)
}
});
数据属性值应该是一个选择器,如果您使用ID,则为#prefix-{{ $data->shipping_id }}
;如果您使用类,则为.prefix-{{ $data->shipping_id }}