我有一系列復選框,可以通過輸入標簽中的禁用來禁用或不禁用
通過單擊父復選框,我只需要檢查那些未禁用的
當前和期望:

到目前為止的代碼:
$("#checkAll").click(function () {
$("input:checkbox").not(this).prop("checked", this.checked);
});
uj5u.com熱心網友回復:
您可以將:not()選擇器與has 屬性選擇器結合使用:
$("#checkAll").change(function () {
$("input:checkbox:not([disabled])").prop("checked", this.checked);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="checkAll" />
<input type="checkbox" />
<input type="checkbox" disabled />
<input type="checkbox" />
<input type="checkbox" disabled />
<input type="checkbox" />
正如@connexo所建議的那樣,您應該使用.change()而不是.click(). 有關更多資訊,請參閱此答案。
uj5u.com熱心網友回復:
Vanilla JS,總是首選:
document.getElementById('checkAll').addEventListener('change', function() {
for (const cb of document.querySelectorAll('input[type=checkbox]:not([disabled])')) {
if (cb !== this) cb.checked = this.checked;
}
});
<input type="checkbox" id="checkAll" />
<input type="checkbox" />
<input type="checkbox" disabled />
<input type="checkbox" />
<input type="checkbox" disabled />
<input type="checkbox" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/407965.html
標籤:
上一篇:如何用限制編號增量
