如何删除.post </option>中的特定<option>

时间:2012-03-22 15:56:10

标签: jquery

这是我的Jquery代码:

$('#unblockButton').click(function () {
                var blockedlist = [];
                var count = 0;
                $('#blockedList > option:selected').each(function () {
                    count = count + 1;
                    blockedlist.push(this.value);
                    $(this).remove();
                });
                unblockDistributor(blockedlist.join(', '), count)
            });

    <select multiple="multiple" class="listBox" name="blockedList" id="blockedList">
        <option title="John Doe (3)" value="21">John Doe (3)</option>
        <option title="Jane Doe (4)" value="24">Jane Doe (4)</option>
        <option title="Jason Doe (5)" value="23">Jason Doe (5)</option>
     </select>

我想要做的是保存自己的页面刷新并删除Jquery中的元素,只有在帖子成功时才更好。

function unblockDistributor(ID, count) {
            var distref = '<%= dictlanguage("DistributorRef") %>';
            var alertStr = ''
            if (count > 1) {
                alertStr = 'You have removed ' + count + ' ' + distref.toLowerCase() + 's from your block list.';
            } else {
                alertStr = 'You have removed ' + count + ' ' + distref.toLowerCase() + ' from your block list.';
            }
            $.post('message_center_functions.asp', { action: "unblock", unblockID: ID }, function (data) {

            })
            .success(function () { alert(alertStr); })
            .error(function () { alert("There was an error while trying to make this request;  If it persists please contact support"); });
        }

3 个答案:

答案 0 :(得分:1)

使用此代码时,只有在$.post成功后才能删除

http://jsfiddle.net/3TVZa/

$('#unblockButton').click(function() {
    var blockedlist = [];
    var count = 0;
    $('#blockedList > option:selected').each(function() {
        count = count + 1;
        blockedlist.push($(this).value);
        $(this).addClass('toBeRemoved');
    });
    unblockDistributor(blockedlist.join(', '), count)
});

function unblockDistributor(ID, count) {
    var distref = '<%= dictlanguage("DistributorRef") %>';
    var alertStr = ''
    if (count > 1) {
        alertStr = 'You have removed ' + count + ' ' + distref.toLowerCase() + 's from your block list.';
    } else {
        alertStr = 'You have removed ' + count + ' ' + distref.toLowerCase() + ' from your block list.';
    }
    $.post('message_center_functions.asp', {
        action: "unblock",
        unblockID: ID
    }, function(data) {

    }).success(function() {
        alert(alertStr);
        $('.toBeRemoved').remove();
    }).error(function() {
        alert("There was an error while trying to make this request;  If it persists please contact support");
        $('.toBeRemoved').removeClass('toBeRemoved');
    });
}

答案 1 :(得分:0)

this.remove更改为$(this).remove();

       $('#unblockButton').click(function () {
            var blockedlist = [];
            var elems = []; // added this array
            var count = 0;
            $('#blockedList > option:selected').each(function () {
                count = count + 1;
                blockedlist.push(this.value);
                elems.push($(this)); // added elem to array
            });
            unblockDistributor(blockedlist.join(', '),elems, count) // passing the elems array
        });

然后

   function unblockDistributor(ID, elems, count) {
            var distref = '<%= dictlanguage("DistributorRef") %>';
            var alertStr = ''
            if (count > 1) {
                alertStr = 'You have removed ' + count + ' ' + distref.toLowerCase() + 's from your block list.';
            } else {
                alertStr = 'You have removed ' + count + ' ' + distref.toLowerCase() + ' from your block list.';
            }
            $.post('message_center_functions.asp', { action: "unblock", unblockID: ID }, function (data) {

            })
            .success(function (){ elems.remove(); //removing the options })
            .error(function () { alert("There was an error while trying to make this request;  If it persists please contact support"); });
        }

答案 2 :(得分:-1)

您可以通过删除方法删除节点,如下所示:

$('#blockedList').remove('[value="' + ID + '"]');