[英]how to disable a button if more than one or no checkbox is checked in React JS
[英]Disable more fields with one checkbox
我有一張桌子,下面的身體。 第一列是一個復選框,其余是從數據庫獲取的信息。 最后兩列是輸入框,我可以根據數量進行更改。 我默認禁用它們。 但是我希望在選中復選框時同時啟用它們,而在未選中時禁用它們。 通過下面的這段代碼,它僅啟用一個ID,而不同時啟用兩個ID。 我該如何實現?
<?php foreach ($this->sklad as $value) : ?>
<tr><td><input type="checkbox" name="checked[]" value="<?= $value['id_item']?>" onclick="enableName(this, 'quantity2<?= $value['id_item']?>');" /></td>
<td><?= $value['id_item']?></td>
<td><?= $value[$en] ?></td>
<td><?= $value['weight'] ?></td>
<td><?= $value['price'] ?></td>
<td><?= $value['code'] ?></td>
<td><?= $value['name'] ?></td>
<td><?= $value['quantity_overall']?></td>
<td><?= $value['quantity_to_sell']?></td>
<td><input type="number" name="n_to_lend" id="quantity2<?= $value['id_item']?>" min="0" max="<?= $value['quantity_overall']?>" disabled><?= $value['quantity_to_lend']?></td>
<td><input type="number" name="repair" id="quantity2<?= $value['id_item']?>" min="0" max="<?= $value['quantity_overall']?>" disabled><?= $value['repair']?></td>
</tr>
<?php endforeach; ?>
</tbody>
*
<script type="text/JavaScript">
function enableName(ctrl, txtId) {
if (ctrl.checked)
$('#' + txtId).removeAttr('disabled');
else
$('#' + txtId).attr('disabled', 'disabled');
}
</script>
*
只需添加要啟用的其他對象?
<script type="text/JavaScript">
function enableName(ctrl, txtId) {
el1 = document.getElementsByName("n_to_lend")
el2 = document.getElementsByName("repair")
if (ctrl.checked){
el1.removeAttr('disabled');
el2.removeAttr('disabled');
}
else
$('#' + txtId).attr('disabled', 'disabled');
}
</script>
並嘗試添加“已啟用”而不是刪除“已禁用”
您需要遍歷所有項目。 傑里米·米勒斯(Jeremy Millers)有一個好的計划
$("input[type='number']").removeAttr("disable"); jQuery
這將選擇您所有的數字輸入。 當然,您可以使用其他選擇器,例如[input:disabled]
親愛的,您似乎不了解類和id選擇的基本行為。 當您傳遞ID作為選擇器時,即使有數百個具有相同ID的元素,它也只會返回第一個匹配的元素。 如果以上是您的最終代碼,那么我建議您使用其他可用的選擇器來達到目的。 例如:
$('input:checkbox').click(function () {
var chk = $(this).is(':checked'));
$('input:number').attr('disabled', !chk);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.