简体   繁体   中英

Disable more fields with one checkbox

I have a table with following body. First column is a checkbox and the rest are information taken from db. The last two columns are input boxes which I can change based on quantity. I have them disabled by default. But I want them BOTH enabled when I tick the checkbox and disabled when unticked. With this code below, it enables only one id, not both. How can I achieve that?

<?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>

*

Just add the other object you want to be enabled?

    <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>

and try adding the 'enabled' rather than removing 'disabled'

You'll need to loop through the items. Jeremy Millers had a good plan

$("input[type='number']").removeAttr("disable"); jQuery 

This selects all your number inputs. Of course you can use other selectors like [input:disabled]

Dear it seems you are unaware of basic behavior of classes and ids selection. When you pass an ID as selector, it will only return the first matching element, even if there are hundreds of more elements with the same id. If above is your final code, then i will suggest you to use other available selectors for your purpose. EX:

$('input:checkbox').click(function () {
    var chk = $(this).is(':checked'));
    $('input:number').attr('disabled', !chk);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM