1

我有两个 ASP 列表框。正如您在下面看到的,lbAvailable 在 PageLoad 上使用 WebMethod 填充并填充所有城市。LbChoosen 的填充取决于 DropDown Value Chosen。下拉菜单有 4 个选项(ALL、Top25、Top50、Top100)。例如,如果您选择值为 4 的前 25 个城市,lbChosen 将填充前 25 个城市(这一切都有效)。

我的问题是 lbAvaliable 总是填充所有城市。因此,如果我选择将前 25 个城市填充到 lbChoosen 中的前 25 个,如何从 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) {
        }
    });
}

}
4

1 回答 1

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();
        }
    }
},
于 2013-08-08T14:19:41.360 回答