简体   繁体   English

未选中复选框时禁用输入

[英]Disable the inputs when checkbox not checked

I have this code and I want to disable all the inputs when the checkbox is not checked. 我有此代码,并且当未选中此复选框时,我想禁用所有输入。

<tr class="select_tr">
    <td><?= ucwords($food_name); ?></td>

    <td class="form-group text-center">
        <input value="<?= $order_food_id; ?>" type="checkbox" name="select_food[]" checked>
    </td>

    <td class="form-group">
        <input value="<?= $order_food_total_members; ?>" type="text" name="total_members[]" class="form-control" placeholder="Total Members">
    </td>

    <td class="form-group ">
        <input value="<?= $order_food_date; ?>" type="date" name="food_date" class="form-control">
    </td>

    <td class="form-group ">
        <input value="<?= $order_food_time; ?>" type="time" name="food_time" class="form-control">
    </td>
</tr>

I have used this code but it is disabling only the first input and I don't know how to taget the remaining two. 我已经使用了这段代码,但是它仅禁用了第一个输入,并且我不知道如何标记其余两个。

$('input[type="checkbox"]').click(function () {
    if(!this.checked) {
        $(this).closest('td').next('td').find('input:text').attr('disabled' , true);
    } else {
        $(this).closest('td').next('td').find('input:text').attr('disabled' , false);
    }
});

You should be able to do something like this - get all of the elements and then disable. 您应该能够执行以下操作-获取所有元素,然后将其禁用。 You'll have to do it onclick of the checkbox. 您必须在复选框上单击一下。

var inputs, index;

inputs = document.getElementsByTagName('input'); // get all of the input elements
for (index = 0; index < inputs.length; ++index) { // loop through them
    inputs[index].disabled = true; // disable them
}

Whatever you have done is partially correct, what you have done is you have targetted with $(this) so obviously it will take the checkbox input and will find the closest 'td' and disable only the first input tag because it is the one which is closest rather you can do this by targetting the element which has this form-control class and disable the event so it will apply to all the input field which has this class which is indirectly the other three input field. 所做的任何事情都是部分正确的,您要做的是使用$(this)定位,因此很明显它将采用复选框输入并找到最接近的“ td”并仅禁用第一个输入标签,因为它是最接近,您可以通过定位具有此表单控制类的元素并禁用事件来执行此操作,以便将其应用于具有该类的所有输入字段,而该输入字段间接是其他三个输入字段。

$('input[type="checkbox"]').click(function () {
    if(!this.checked) {
        $('.form-control').attr('disabled' , true);
    } else {
        $('.form-control').attr('disabled' , false);
    }
});

Or the second choice is you can use this each statement to update the input element. 或者第二种选择是可以使用每个语句来更新输入元素。

The issue is that you are only selecting a single element: 问题是您只选择一个元素:

$(this).closest('td').next('td').find('input:text')

This will find the closest <td> ancestor element to the checkbox, then select the next sibling <td> element, and finally get the descendants of that <td> element that matches the selector input:text . 这将找到最接近复选框的<td>祖先元素,然后选择下一个同级<td>元素,最后得到与选择器input:text匹配的该<td>元素的后代。 This will result in a single element because only one of your inputs is of type "text", and you are only selecting inside of a single <td> . 这将导致单个元素,因为只有一个输入属于“ text”类型,并且您仅在单个<td>内部进行选择。

What you want to do instead is select all of the input elements that are not checkboxes in the table row <tr> (class .select_tr ): 您要做的是在表行<tr> (类.select_tr )中选中所有非复选框的input元素:

$(this).closest('.select_tr').find('input:not([type=checkbox])')

This will select the parent <tr> with class .select_tr and then select all input children of it that do not have type=checkbox . 这将选择具有类.select_tr的父<tr> ,然后选择其所有没有type=checkbox input子级。

Example: 例:

 $('.select_tr input:checkbox').change(function() { $(this).closest('.select_tr').find('input:not([type=checkbox])').prop("disabled", !this.checked); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr class="select_tr"> <td> <?= ucwords($food_name); ?> </td> <td class="form-group text-center"> <input value="<?= $order_food_id; ?>" type="checkbox" name="select_food[]" checked> </td> <td class="form-group"> <input value="<?= $order_food_total_members; ?>" type="text" name="total_members[]" class="form-control" placeholder="Total Members"> </td> <td class="form-group "> <input value="<?= $order_food_date; ?>" type="date" name="food_date" class="form-control"> </td> <td class="form-group "> <input value="<?= $order_food_time; ?>" type="time" name="food_time" class="form-control"> </td> </tr> </table> 

You can try something like this: 您可以尝试如下操作:

var elemCheckbox = document.querySelectorAll('[type="checkbox"]')[0];
var elemInputs = document.querySelectorAll('.form-control');

function toggleInputs() {
    // https://stackoverflow.com/questions/9887360/check-if-checkbox-is-checked-javascript#answer-9887439
    var shallDisable = !elemCheckbox.checked;
    // https://css-tricks.com/snippets/javascript/loop-queryselectorall-matches/
    for (i = 0, len = elemInputs.length; i < len; ++i) {
        // https://stackoverflow.com/questions/7526601/setattributedisabled-false-changes-editable-attribute-to-false#answer-7526666
        elemInputs[i].disabled = shallDisable;
    }
}

elemCheckbox.addEventListener("click", toggleInputs);

Find the explanation in the comments. 在评论中找到解释。 This example won't disable the checkbox (if you meant it like that). 本示例将不会禁用该复选框(如果您是这样表示的话)。

I would recommend to add an id to the checkbox. 我建议向该复选框添加一个ID。 This will guarantee an explicit element selection. 这将保证明确的元素选择。

You can then get like document.getElementById("<yourUniqueId>"); 然后,您可以像获得document.getElementById("<yourUniqueId>");

Your code isn't selecting all the inputs . 您的代码未选择所有inputs Use nextAll() for selecting each td after the closest('td'). 使用nextAll()选择closest('td').之后的每个td closest('td').

$('input[type="checkbox"]').click(function(){
 if(!this.checked){
  $(this).closest('td').nextAll('td').find('input').attr('disabled' , true);
 }else{
  $(this).closest('td').nextAll('td').find('input').attr('disabled' , false);
 }
});

JQuery.next() is used for selecting the first matched element after the selected element JQuery.next()用于选择所选元素之后的第一个匹配元素

So, your code is just affecting the first next only. 因此,您的代码仅影响第一个。

JQuery.nextAll() is used for select the next all matched elements after the selected element. JQuery.nextAll()用于选择所选元素之后的下一个所有匹配的元素。

So, using nextAll() instead of next() may solve your problem. 因此,使用nextAll()代替next()可能会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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