繁体   English   中英

使用复选框禁用表格行的输入,反之亦然

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

单击复选框后,我想禁用表格中一行的输入,但该复选框仍处于活动状态,以便我可以再次激活一行的输入。 并且在注册表格后,当我再次返回该表格时,如果选中该复选框,则同一行的输入将被禁用。 我写了一些代码,但请完成它。 谢谢

 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>

在此处输入图像描述

你的复选框也是输入的,如果你不想禁用自己,你可以 select 如下: find("input[type='text']")

如果你想再次激活它,你可以尝试: removeAttr("disabled")

我尝试如下:

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

结果:

在此处输入图像描述

暂无
暂无

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

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