簡體   English   中英

在 Javascript 中啟用/禁用 Gridview (ASP.NET/ASCX) 中的文本框

[英]Enable/disable text box in Gridview (ASP.NET/ASCX) in Javascript

我的代碼只需要一點幫助。 我希望在未選中復選框時禁用文本框! 你能幫我解決這個問題嗎? 而且,如果用戶刪除了一個檢查,我希望再次啟用文本框(非常感謝)。 這是我的代碼:

    <asp:GridView ID="gvModifOuvrageNonControles" runat="server" AutoGenerateColumns="false" SkinID="MarionGridView">
        <Columns>
            <asp:BoundField DataField="MirePrincipal" HeaderText="OUVRAGE PRINCIPAL" />
            <asp:BoundField DataField="LibelleMireSecondaire" HeaderText="OUVRAGE SECONDAIRE" />
            <asp:TemplateField HeaderText="NON CONTROLE">
                <ItemTemplate>
                    <asp:CheckBox ID="cbInspection" OnClick="grisé(this);" runat="server" />
                </ItemTemplate>
            </asp:TemplateField> 
            <asp:BoundField DataField="Libellel" HeaderText="LIBELLE DES MS,VI,PI,SU,CP,PL,PF" />
            <asp:TemplateField HeaderText="RAISON">
                <ItemTemplate>
                    <asp:TextBox ID="txtCause"  runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div> ``'

<script type="text/javascript">

                function grisé(obj) {
                    var rowData = obj.parentNode.parentNode;
                    //0-based 
                    // use as: (YourTemplateFieldColumnWhichContainsThatTextBox -1)
                    rowData.cells[4].firstElementChild.disabled= true;
                    rowData.cells[2].firstElementChild.checked= false;
                }

</script>



<html>
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="gvModifOuvrageNonControles" runat="server" AutoGenerateColumns="false"
        SkinID="MarionGridView">
        <Columns>
            <asp:BoundField DataField="MirePrincipal" HeaderText="OUVRAGE PRINCIPAL" />
            <asp:BoundField DataField="LibelleMireSecondaire" HeaderText="OUVRAGE SECONDAIRE" />
            <asp:TemplateField HeaderText="NON CONTROLE">
                <ItemTemplate>
                    <asp:CheckBox ID="cbInspection" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Libellel" HeaderText="LIBELLE DES MS,VI,PI,SU,CP,PL,PF" />
            <asp:TemplateField HeaderText="RAISON">
                <ItemTemplate>
                    <asp:TextBox ID="txtCause" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

    <script type="text/javascript">
        $(function() {
            //Enable Disable all TextBoxes when Header Row CheckBox is checked.
            $("[id*=cbInspection]").bind("click", function() {
                var cbInspection = $(this);

                //Find and reference the GridView.
                var grid = $(this).closest("table");

                //Loop through the CheckBoxes in each Row.
                $("td", grid).find("input[type=checkbox]").each(function() {

                    //If Header CheckBox is checked.
                    //Then check all CheckBoxes and enable the TextBoxes.
                    if (cbInspection.is(":checked")) {
                        $(this).attr("checked", "checked");
                        var td = $("td", $(this).closest("tr"));
                        td.css({ "background-color": "#D8EBF2" });
                        $("input[type=text]", td).removeAttr("disabled");
                    } else {
                        $(this).removeAttr("checked");
                        var td = $("td", $(this).closest("tr"));
                        td.css({ "background-color": "#FFF" });
                        $("input[type=text]", td).attr("disabled", "disabled");
                    }
                });
            });
        });
    </script>

    <script type="text/javascript">
        $(function() {
            //Enable Disable TextBoxes in a Row when the Row CheckBox is checked.
            $("[id*=chkRow]").bind("click", function() {

                //Find and reference the GridView.
                var grid = $(this).closest("table");

                //Find and reference the Header CheckBox.
                var cbInspection = $("[id*=cbInspection]", grid);

                //If the CheckBox is Checked then enable the TextBoxes in thr Row.
                if (!$(this).is(":checked")) {
                    var td = $("td", $(this).closest("tr"));
                    td.css({ "background-color": "#FFF" });
                    $("input[type=text]", td).attr("disabled", "disabled");
                } else {
                    var td = $("td", $(this).closest("tr"));
                    td.css({ "background-color": "#D8EBF2" });
                    $("input[type=text]", td).removeAttr("disabled");
                }

                //Enable Header Row CheckBox if all the Row CheckBoxes are checked and vice versa.
                if ($("[id*=chkRow]", grid).length == $("[id*=chkRow]:checked", grid).length) {
                    cbInspection.attr("checked", "checked");
                } else {
                    cbInspection.removeAttr("checked");
                }
            });
        });
    </script>

    </form>
</body>
</html>

這里: - 我是禁用和啟用該Gridview中的文本框的Jquery ...

您可以點擊此鏈接獲取信息....它將對您有很大幫助.....

https://www.aspsnippets.com/Articles/Enable-disable-TextBox-in-GridView-when-checkbox-is-selected-checked-in-ASPNet.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM