我有幾個復選框分布在多個 div 中。在所有單獨的 div 中,我有一個“全選”復選框。當該復選框被選中時,我試圖選擇與該 pertucular div 相關的所有其他復選框,當您取消選擇“全選”一個時,它會取消選擇其他的。
<div class="actions" id="actions" title="Actions">
<div>
<div><input type="checkbox" name="action" id="" class="" value="0" /> Select All</div><br />
<div><input type="checkbox" name="action" id="" class="" value="1" /> <?php echo $lang["actions"][1]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="2" /> <?php echo $lang["actions"][2]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="3" /> <?php echo $lang["actions"][3]; ?></div><br />
</div>
<div>
<div><input type="checkbox" name="action" id="" class="" value="26" />Select All</div><br />
<div><input type="checkbox" name="action" id="" class="" value="27" /> <?php echo $lang["actions"][27]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="28" /> <?php echo $lang["actions"][28]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="29" /> <?php echo $lang["actions"][29]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="30" /> <?php echo $lang["actions"][30]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="31" /> <?php echo $lang["actions"][31]; ?></div>
</div>
</div>
uj5u.com熱心網友回復:
這是一個例子
下次至少給我們展示最少的代碼,以便我們幫助您理解
$('.selectAll').on('click',function(){
let isSelected = $(this).is(':checked');
$(this).parents('.myDiv').find('input[type="checkbox"]').each(function(){
if( isSelected )
$(this).prop('checked',true);
else
$(this).prop('checked',false);
})
})
$('input:not(".selectAll")').on('click',function(){
let selectAllChecked = $(this).parents('.myDiv').find('.selectAll')[0].checked;
let numberInput = $(this).parents('.myDiv').find('input').not('.selectAll').length;
let numberInputChecked = $(this).parents('.myDiv').find('input:checked').not('.selectAll').length;
if( selectAllChecked )
$(this).parents('.myDiv').find('.selectAll')[0].checked = false;
if( numberInput == numberInputChecked)
$(this).parents('.myDiv').find('.selectAll')[0].checked = true;
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="actions" id="actions" title="Actions">
<div class="myDiv">
<div><input type="checkbox" name="action" id="" class="selectAll" value="0" /> Select All</div><br />
<div><input type="checkbox" name="action" id="" class="" value="1" /> <?php echo $lang["actions"][1]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="2" /> <?php echo $lang["actions"][2]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="3" /> <?php echo $lang["actions"][3]; ?></div><br />
</div>
<div class="myDiv">
<div><input type="checkbox" name="action" id="" class="selectAll" value="26" />Select All</div><br />
<div><input type="checkbox" name="action" id="" class="" value="27" /> <?php echo $lang["actions"][27]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="28" /> <?php echo $lang["actions"][28]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="29" /> <?php echo $lang["actions"][29]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="30" /> <?php echo $lang["actions"][30]; ?></div><br />
<div><input type="checkbox" name="action" id="" class="" value="31" /> <?php echo $lang["actions"][31]; ?></div>
</div>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/334626.html
標籤:javascript php html 查询
上一篇:使用preventDefault函式后,有沒有辦法提交此表單?
下一篇:跟蹤按下了哪個按鈕?
