CheckboxList在ASP.NET中选中和取消选中

时间:2017-09-28 04:26:18

标签: javascript c# jquery asp.net

我有三个与数据库列表绑定的Checkbox List。从数据库中获取的值是不同的值。在复选框与数据绑定后,我必须选中一个复选框。如果我从第一个复选框列表类别中选中一个复选框,它应该在第二个和第三个复选框列表中显示匹配的值。

示例:

CheckboxList 1:
1.100
2.200
3.300
4.400

CheckboxList 2:
1.powder
2.Capsule
3.gel

CheckboxList 3:
1.10
2.30
3.30

如果我从checkboxlist 1中选择100,那么它应该只从checkboxlist2启用粉末和胶囊,并从checkboxlist 3启用10并禁用其他,如果我从复选框1中选择多个选项,则应根据选择启用复选框。

1 个答案:

答案 0 :(得分:0)

您可以使用JavaScript(我使用jQuery)来避免刷新页面。

我定义了一种矩阵,对应于第一个复选框列表的不同选择。

第一个复选框列表中的更改(选中复选框)会触发一个函数,该函数会从此复选框列表中选中所有复选框。然后检查值,例如01获取矩阵matrix["01"]中的相应选项:0|1;第二个复选框列表将启用索引0处的复选框,第三个复选框列表将启用索引1处的复选框。

您可以在我的答案底部尝试/运行HTML版本。

您可以在WebForm中尝试以下代码:

ASPX:

<asp:CheckBoxList ID="CheckBoxList1" onchange="manageCheckboxList();" runat="server">
    <asp:ListItem Value="0" Text="100"></asp:ListItem>
    <asp:ListItem Value="1" Text="200"></asp:ListItem>
    <asp:ListItem Value="2" Text="300"></asp:ListItem>
    <asp:ListItem Value="3" Text="400"></asp:ListItem>
</asp:CheckBoxList>

<hr />

<asp:CheckBoxList ID="CheckBoxList2" runat="server">
    <asp:ListItem Value="0" Text="Powder"></asp:ListItem>
    <asp:ListItem Value="1" Text="Capsule"></asp:ListItem>
    <asp:ListItem Value="2" Text="Gel"></asp:ListItem>
</asp:CheckBoxList>

<hr />

<asp:CheckBoxList ID="CheckBoxList3" runat="server">
    <asp:ListItem Value="0" Text="10"></asp:ListItem>
    <asp:ListItem Value="1" Text="20"></asp:ListItem>
    <asp:ListItem Value="2" Text="30"></asp:ListItem>
</asp:CheckBoxList>

JavaScript(jQuery):

function manageCheckboxList() {
    // Get checkbox checked from first checkbox list
    var checked = $('#CheckBoxList1').find('input:checked');

    // Check if there's a least one checkbox checked
    if (checked.length > 0) {
        var matrix = {}
        matrix["0"] = "01|0";
        matrix["1"] = "12|01";
        matrix["2"] = "2|2";
        matrix["3"] = "1|0";
        matrix["01"] = "0|1";
        matrix["02"] = "0|2";
        matrix["03"] = "1|1";
        matrix["12"] = "1|2";
        matrix["13"] = "0|2";
        matrix["23"] = "12|12";
        matrix["012"] = "012|012";
        matrix["013"] = "012|012";
        matrix["023"] = "01|01";
        matrix["123"] = "12|02";
        matrix["0123"] = "012|012";

        var result = "";

        // Construct result
        $(checked).each(function () {
            result += $(this).val();
        });

        var subchoices = matrix[result].split('|');

        // Update Checkbox list 2 and 3
        updateCheckboxList($("#CheckBoxList2"), subchoices[0], false);
        updateCheckboxList($("#CheckBoxList3"), subchoices[1], false);
    } else {
        // Reset checkbox
        updateCheckboxList($("#CheckBoxList2"), null, false);
        updateCheckboxList($("#CheckBoxList3"), null, false);
    }
}

function updateCheckboxList(el, subchoices, disableCheckboxList) {
    var checkboxList = $(el).find("input");

    for (var i = 0; i < checkboxList.length; i++) {
        if (subchoices != null) {
            var disabled = true;

            if (subchoices.indexOf($(checkboxList[i]).val()) >= 0) {
                disabled = false;
            }

            $(checkboxList[i]).prop("disabled", disabled);
        } else {
            $(checkboxList[i]).prop("disabled", false);
        }
    }

    $(el).prop("disabled", disableCheckboxList);
}

请尝试使用以下代码段(使用HTML而不是ASPX)查看结果:

&#13;
&#13;
function manageCheckboxList() {
    // Get checkbox checked from first checkbox list
    var checked = $('#CheckBoxList1').find('input:checked');

    // Check if there's a least one checkbox checked
    if (checked.length > 0) {
        var matrix = {}
        matrix["0"] = "01|0";
        matrix["1"] = "12|01";
        matrix["2"] = "2|2";
        matrix["3"] = "1|0";
        matrix["01"] = "0|1";
        matrix["02"] = "0|2";
        matrix["03"] = "1|1";
        matrix["12"] = "1|2";
        matrix["13"] = "0|2";
        matrix["23"] = "12|12";
        matrix["012"] = "012|012";
        matrix["013"] = "012|012";
        matrix["023"] = "01|01";
        matrix["123"] = "12|02";
        matrix["0123"] = "012|012";

        var result = "";

        // Construct result
        $(checked).each(function () {
            result += $(this).val();
        });

        var subchoices = matrix[result].split('|');

        // Update Checkbox list 2 and 3
        updateCheckboxList($("#CheckBoxList2"), subchoices[0], false);
        updateCheckboxList($("#CheckBoxList3"), subchoices[1], false);
    } else {
        // Reset checkbox
        updateCheckboxList($("#CheckBoxList2"), null, false);
        updateCheckboxList($("#CheckBoxList3"), null, false);
    }
}

function updateCheckboxList(el, subchoices, disableCheckboxList) {
    var checkboxList = $(el).find("input");

    for (var i = 0; i < checkboxList.length; i++) {
        if (subchoices != null) {
            var disabled = true;

            if (subchoices.indexOf($(checkboxList[i]).val()) >= 0) {
                disabled = false;
            }

            $(checkboxList[i]).prop("disabled", disabled);
        } else {
            $(checkboxList[i]).prop("disabled", false);
        }
    }

    $(el).prop("disabled", disableCheckboxList);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="CheckBoxList1" onchange="manageCheckboxList();">
    <tr>
        <td>
            <input id="CheckBoxList1_0" type="checkbox" value="0" />
            <label for="CheckBoxList1_0">100</label>
        </td>
    </tr>
    <tr>
        <td>
            <input id="CheckBoxList1_1" type="checkbox" value="1" />
            <label for="CheckBoxList1_1">200</label>
        </td>
    </tr>
    <tr>
        <td>
            <input id="CheckBoxList1_2" type="checkbox" value="2" />
            <label for="CheckBoxList1_2">300</label>
        </td>
    </tr>
    <tr>
        <td>
            <input id="CheckBoxList1_3" type="checkbox" value="3" />
            <label for="CheckBoxList1_3">400</label>
        </td>
    </tr>
</table>

<hr />

<table id="CheckBoxList2">
    <tr>
        <td>
            <input id="CheckBoxList2_0" type="checkbox" value="0" />
            <label for="CheckBoxList2_0">Powder</label>
        </td>
    </tr>
    <tr>
        <td>
            <input id="CheckBoxList2_1" type="checkbox" value="1" />
            <label for="CheckBoxList2_1">Capsule</label>
        </td>
    </tr>
    <tr>
        <td>
            <input id="CheckBoxList2_2" type="checkbox" value="2" />
            <label for="CheckBoxList2_2">Gel</label>
        </td>
    </tr>
</table>

<hr />

<table id="CheckBoxList3">
    <tr>
        <td>
            <input id="CheckBoxList3_0" type="checkbox" value="0" />
            <label for="CheckBoxList3_0">10</label>
        </td>
    </tr>
    <tr>
        <td>
            <input id="CheckBoxList3_1" type="checkbox" value="1" />
            <label for="CheckBoxList3_1">20</label>
        </td>
    </tr>
    <tr>
        <td>
            <input id="CheckBoxList3_2" type="checkbox" value="2" />
            <label for="CheckBoxList3_2">30</label>
        </td>
    </tr>
</table>
&#13;
&#13;
&#13;

相关问题