使用jquery从清单中删除项目

时间:2014-01-22 07:51:39

标签: jquery

我有一个asp.net网页。

我使用的是asp.net checkboxlist控件。

是否可以使用jquery删除项目?

我可以枚举已检查的数组,但似乎无法删除已检查的项目。

我有这个不起作用:

       function ConfirmDialog(message) {
        $('<div></div>').appendTo('body')
                .html('<div><h6>' + message + '?</h6></div>')
                .dialog({
                    modal: true, title: 'Delete', zIndex: 10000, autoOpen: true,
                    width: 'auto', resizable: false,
                    buttons: {
                        Yes: function () {
                            var selectedItems = "";
                            $("[id*=chkEmailClients] input:checked").each(function () {
                                var emailClient = $(this).next().html();

                                //                                    jQuery(this).closest('chkEmailClients.tr').remove();

                                alert($(this).closest('tr').html());
                                try {
                                    $(this).closest('tr').remove();
                                    return false;
                                }
                                catch (error) {
                                    alert(error);
                                }
                            });
                            $(this).dialog("close");
                        },
                        No: function () {
                            $(this).dialog("close");
                        }
                    },
                    close: function (event, ui) {
                        $(this).remove();
                    }
                });
    };

这是DOM的东西:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div>
        <span id="chkEmailClients" style="display: inline-block; color: Black; font-family: Verdana;
            font-size: 8pt; height: 200px; width: 270px;">
            <input id="chkEmailClients_0" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_0"
                value="andrew.simpson18@hotmail.co.uk"><label for="chkEmailClients_0">email1@hotmail.co.uk</label><br>
            <input id="chkEmailClients_1" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_1"
                value="andrew.simpson17@hotmail.co.uk"><label for="chkEmailClients_1">email2@hotmail.co.uk</label><br>
            <input id="chkEmailClients_2" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_2"
                value="andrew.simpson1712@virginmedia.com"><label for="chkEmailClients_2">email3@virginmedia.com</label><br>
            <input id="chkEmailClients_3" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_3"
                value="andrew.simpson18@hotmail.co.uk"><label for="chkEmailClients_3">email4@hotmail.co.uk</label><br>
            <input id="chkEmailClients_4" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_4"
                checked="checked" value="andrew.simpson17@hotmail.co.uk"><label for="chkEmailClients_4">email5@hotmail.co.uk</label><br>
            <input id="chkEmailClients_5" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_5"
                value="andrew.simpson1712@virginmedia.com"><label for="chkEmailClients_5">email6@virginmedia.com</label><br>
            <input id="chkEmailClients_6" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_6"
                value="andrew.simpson18@hotmail.co.uk"><label for="chkEmailClients_6">email7@hotmail.co.uk</label><br>
            <input id="chkEmailClients_7" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_7"
                value="andrew.simpson17@hotmail.co.uk"><label for="chkEmailClients_7">email8@hotmail.co.uk</label><br>
            <input id="chkEmailClients_8" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_8"
                value="andrew.simpson1712@virginmedia.com"><label for="chkEmailClients_8">email9@virginmedia.com</label><br>
            <input id="chkEmailClients_9" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_9"
                value="andrew.simpson18@hotmail.co.uk"><label for="chkEmailClients_9">email10@hotmail.co.uk</label><br>
            <input id="chkEmailClients_10" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_10"
                value="andrew.simpson17@hotmail.co.uk"><label for="chkEmailClients_10">email11@hotmail.co.uk</label><br>
            <input id="chkEmailClients_11" type="checkbox" name="ctl00$MainContent$chkEmailClients$chkEmailClients_11"
                value="andrew.simpson1712@virginmedia.com"><label for="chkEmailClients_11">email12@virginmedia.com</label></span>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

asp.net从您的复选框列表中生成一个表,并且为了删除一个选项,您必须实际删除TR。在循环中添加此代码。

 $(this).closest('tr').remove();
 return false;

<强> See this

答案 1 :(得分:0)

对任何感兴趣的人。 @Milind Anantwar提供的答案是正确的。我已将Repeatlayout设置为flow,这意味着渲染的html处于输入/标签模式。通过更改为表格,它工作。我累的时候不应该编程