简体   繁体   中英

Disabling the inputs of a table row with a checkbox and vice versa

I want to disable the inputs of one row of the table after clicking on the checkbox, but the checkbox remains active so that I can activate the inputs of one row again. And after registering the form, when I return to this form again, if the check box was checked, the inputs of the same row will be disabled. I wrote some code, but please complete it. Thank you

 function DisableRow(checkBox) { var $trElement = $(checkBox).closest("tr"); $trElement.find("input").attr("disabled", "disabled"); $trElement.find("select").attr("disabled", "disabled"); } $(document).ready(function() { if ($('#checkBox').is(':checked')) { } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td> <input class="form-control mt-1 text-sm " disabled value="@Model.ToList()[i].MablaghKhreedAnjamShode" onkeyup="javascript:this.value=itpro(this.value);" name="[@i].Saiar" required autocomplete="off" /> <input class="d-none" name="[@i].ProjectId" /> <input class="d-none" name="id" value="@Model.ToList()[i].ProjectId" /> <input class="form-control d-none" type="number" name="ListID" value="@Model.ToList()[i].ListId" /> </td> <td> <label class="switch"> <input id="theCheckBox" type="checkbox" onchange="DisableRow(this)" asp-for="@Model.ToList()[i].VazeetKhareedDarInFasle" value="True"> <span class="slider round"></span> </label> </td> <td> <select class=" mt-1 form-control" asp-for="@Model.ToList()[i].NoaeFactor" style="font-size: 12px; width: 130px" required autocomplete="off"> <option value="" default="" selected="">انتخاب کنید</option> <option value="1">خرید</option> <option value="2">برگشت از خرید</option> </select> </td> </tr> </table>

在此处输入图像描述

Your checkbox is input also,if you don't want to disable itself,you could select as below: find("input[type='text']")

If you want to active it again,you could try: removeAttr("disabled")

I tried as below:

<table>
    <tr>
        <td>
            <input type="text" id="tr1input1"/>
        </td>
        <td>
            <input type="text" id="tr1input2"/>
        </td>
        <td>
            
            <input type="checkbox" id="checkbox1" onchange="DisableRow(this)" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="text" id="tr2input1"/>
        </td>
        <td>
            <input type="text" id="tr2input2"/>
        </td>
        <td>
            
            <input type="checkbox"  id="checkbox2" onchange="DisableRow(this)" />
        </td>
    </tr>
</table>

<script>    
    
    function DisableRow(e) {
        var targetelement = $(e).closest('tr').find("input[type='text']")
        if (e.checked == true)
        {
            
            targetelement.attr("disabled", "disabled")
        }
        else
        {
            targetelement.removeAttr("disabled")
        }
    }
</script>
<script>
    window.onload=function () {
       
        $("#checkbox2").attr("checked", true)
        
        if( $("#checkbox2").is(':checked'))
        {
            $("#checkbox2").closest('tr').find("input[type='text']").attr("disabled", "disabled")
            
        }
        
    }
</script>

Result:

在此处输入图像描述

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