2 回答
TA貢獻1836條經驗 獲得超13個贊
我假設以下內容:
my_value(復選框的最大值)設置在此代碼之外的某處表中具有 name 屬性的所有復選框
FW_check看起來像您在上面發布的輸入元素(主要意味著它們value是有效數字)
也就是說,我建議您執行以下操作:
獲取表中每個
input[name="FW_check"]的列表,使用document.querySelector遍歷這些復選框,檢查
value輸入,并設置disabled = true該值是否大于最大值。
這是一些可以完成此操作的基本代碼:
const my_value = // some number that you have defined elsewhere, as mentioned above
const checkboxes = document.querySelector('.table input[name="FW_check"]');
for (const checkbox of checkboxes) {
const value = Number.parseInt(checkbox.value, 10); // note the base 10. in case you accidentally put a 0 before a number, it still parses correctly and doesn't assume base 8
checkbox.disabled = value > my_value; // this will handle re-enabling the checkbox if this function is run again after my_value drops
}
這當然可以根據需要運行多次,放入函數等。
TA貢獻1864條經驗 獲得超2個贊
如果我正確理解你的問題,這就是你要找的。
var max = 5;
var checkboxes = document.getElementsByName("FW_check");
for(var i=0;i<checkboxes.length;i++){
if(i<max){
checkboxes[i].disabled = false;
} else {
checkboxes[i].disabled = true;
}
}
<div class="container">
<table class="table">
<thead>
<tr>
<th align="left">Max Number of Checks</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input id="Check_01" name="FW_check" value="1" type="checkbox" />
<input id="Check_02" name="FW_check" value="2" type="checkbox" />
<input id="Check_03" name="FW_check" value="3" type="checkbox" />
<input id="Check_04" name="FW_check" value="4" type="checkbox" />
<input id="Check_05" name="FW_check" value="5" type="checkbox" />
</td>
</tr>
<tr>
<td>
<input id="Check_06" name="FW_check" value="6" type="checkbox" />
<input id="Check_07" name="FW_check" value="7" type="checkbox" />
<input id="Check_08" name="FW_check" value="8" type="checkbox" />
<input id="Check_09" name="FW_check" value="9" type="checkbox" />
<input id="Check_10" name="FW_check" value="10" type="checkbox" />
</td>
</tr>
</tbody>
</table>
</div>
添加回答
舉報
