任何人都可以给Javascript/jQuery在点击按钮时从ASP:CheckBoxList中删除元素

Can any one give Javascript/jQuery to remove elements from ASP:CheckBoxList on button click?

本文关键字:ASP CheckBoxList 元素 删除 按钮 Javascript 都可以 jQuery 任何人      更新时间:2023-09-26

假设列表中有3个元素

<asp:CheckBoxList ID="check" runat="server">
            <asp:ListItemText="Item 1" Value="1"></asp:ListItem>
            <asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
            <asp:ListItem Text="Item 3" Value="3"></asp:ListItem>
        </asp:CheckBoxList>

如果用户单击按钮,则最后一个元素(项目3)应当被移除,并且如果用户再次单击,则另外的项目2应当被移除。

这应该对您有效。只需将按钮的onclick属性设置为指向此函数即可。

JSFiddle:http://jsfiddle.net/tdg4k07m/

    function remove_last_element()
    {
        var list = document.getElementById('check');
        if (list.children.length > 0)
            list.removeChild(list.children[list.children.length - 1]);
    }