两个具有相同值的列表框,我需要从一个列表框中删除identicial值

时间:2013-08-08 13:49:09

标签: c# javascript asp.net jquery

我有两个ASP ListBox。如下所示,使用WebMethod在PageLoad上填充lbAvailable并填充所有城市。根据DropDown Value Chosen填充LbChoosen。 Dropdown有4个选项(ALL,Top25,Top50,Top100)。例如,如果您选择前25位值4,则lbChosen将填充前25个城市(这一切都有效)。

我的问题是lbAvaliable总是填充所有城市。因此,如果我选择排名前25位的前25名城市进入lbChoosen,那么如何从lbAvailable中删除这些价值(前25个城市)

function LoadMarketsAvailableJS() {
var ddlFootprint = $('#ddlFootprint');
var lbChoosen = $('#lbChoosen');
var lbAvailable = $('#lbAvailable');
lbChoosen.empty();

var SelectedMarkets = [];
var url = "";

 //Load lbAvailable on Page Load with all Markets
 $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "Campaign.aspx/LoadAvailableMarkets",
    dataType: "json",
    success: function (msg) {
    var obj = $.parseJSON(msg.d);
    for (var i = 0; i < obj.Markets.length; i++) {
        if (SelectedMarkets.indexOf(obj.Markets[i].id.toString()) == -1) {
    $("#lbAvailable").append($("<option></option>")
   .attr("value",          obj.Markets[i].id)
   .text(obj.Markets[i].name + " - " + obj.Markets[i].rank));

    }
        }
            },
    error: function(result) {
    alert("Error");
    }
});


//Check DropdownList
if (parseInt(ddlFootprint.val()) == 1) {
    url = 'Campaign.aspx/LoadAvailableMarkets';
} else if (parseInt(ddlFootprint.val()) == 2) {
    url = 'Campaign.aspx/LoadTop100Markets';
}
else if (parseInt(ddlFootprint.val()) == 3) {
    url = 'Campaign.aspx/LoadTop50Markets';
}
else if (parseInt(ddlFootprint.val()) == 4) {
    url = 'Campaign.aspx/LoadTop25Markets';
}
else if (parseInt(ddlFootprint.val()) == 5) {
    url = 'Campaign.aspx/LoadAvailableMarkets';
}
//Load Select Dropdown Value to lbChoosen
if (url.length > 0) {
    $.ajax({
        type: "POST",
        url: url,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var obj = $.parseJSON(msg.d);
            for (var i = 0; i < obj.Markets.length; i++) {
                if (SelectedMarkets.indexOf(obj.Markets[i].id.toString()) == -1) {
                    lbChoosen
                        .append($("<option></option>")
                        .attr("value", obj.Markets[i].id)
                        .text(obj.Markets[i].name + " - " + obj.Markets[i].rank));
                }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
        },
        complete: function (jqXHR, textStatus) {
        }
    });
}

}

1 个答案:

答案 0 :(得分:1)

假设我已经理解了你要问的内容,如果你想从lbAvailable中删除选项,因为它们被添加到lbChoosen中你应该添加以下行:

lbAvailable.find('option[value="' + obj.Markets[i].id + '"]').remove();

所以你的代码看起来像是:

success: function (msg) {
    var obj = $.parseJSON(msg.d);
    for (var i = 0; i < obj.Markets.length; i++) {
        if (SelectedMarkets.indexOf(obj.Markets[i].id.toString()) == -1) {
            lbChoosen
                .append($("<option></option>")
                .attr("value", obj.Markets[i].id)
                .text(obj.Markets[i].name + " - " + obj.Markets[i].rank));
            lbAvailable.find('option[value="' + obj.Markets[i].id + '"]').remove();
        }
    }
},