使用JavaScript在下拉菜单中保存价值

时间:2018-07-19 18:43:17

标签: javascript c# jquery asp.net jquery-select2

我想在表单编辑时在“国家”>“州”>“城市”>“区域”的嵌套下拉列表中设置默认值。 所有数据都填充在所有下拉列表中,但未设置该值。

我使用过select2下拉列表,并使用asp.net形式的jquery ajax填充数据。

================================================ ======================

UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!

================================================ ======================

function Edit()
{
 $(response.d).find("tblInvoice").each(function () {
    selCountryId = $(this).find("intCountryId").text();
    $("#select2-selCountry-container").text($(this).find("strCountryName").text());
    $('#selCountry').trigger('change');

    selStateId = $(this).find("intStateId").text();
    $('#selState').trigger('change');
    $("#select2-selState-container").text($(this).find("strStateName").text());

    selCityId = $(this).find("intCityId").text();
    $('#selCity').trigger('change');
    $("#select2-selCity-container").text($(this).find("strCityName").text());
}

================================================ ======================

function getCountry() {
    $.ajax({
        type: "POST",
        url: "Country.aspx/GetCountry",
        data: JSON.stringify({}),
        contentType: "application/json; charset=utf-8",
        dataType: "JSON",
        success: function (response) {

            var name = "#selCountry";
            var ddl = $(name);
            var Col_Key = "intCountryID";
            var Col_val = "strCountryName";

            ddl.find('option').remove();
            $(response.d).find("tblCountry").each(function () {
                var OptionValue = $(this).find(Col_Key).text();
                var OptionText = $(this).find(Col_val).text();
                var option = $("<option>" + OptionText + "</option>");
                option.attr("value", OptionValue);
                ddl.append(option);
            });
            $(ddl).val('0');
        },
        failure: function (response) {
        }

    });
});
}

================================================ ======================

function getState() {
    $.ajax({
        type: "POST",
        url: "State.aspx/GetState",
        data: JSON.stringify({
            COUNTRYID: countryid,
            ACTION: Action
        }),
        contentType: "application/json; charset=utf-8",
        dataType: "JSON",
        success: function (response) {
            var name = "#selState";
            var ddl = $(name);
            var Col_Key = "intStateID";
            var Col_val = "strStateName";

            ddl.find('option').remove();
            $(response.d).find("tblState").each(function () {
                var OptionValue = $(this).find(Col_Key).text();
                var OptionText = $(this).find(Col_val).text();
                var option = $("<option>" + OptionText + "</option>");
                option.attr("value", OptionValue);
                ddl.append(option);
            });
            $(ddl).val('0');
        },
        failure: function (response) {
        }
    });
}

================================================ ======================

1 个答案:

答案 0 :(得分:0)

function Edit()
{
var intddlCountry = '0', strddlCountry = '0', intddlState = '0', strddlState = '0', intddlCity = '0', strddlCity = '0';

$(response.d).find("tblInvoice").each(function () {

intddlCountry = $(this).find("intCountryId").text();
strddlCountry = $(this).find("strCountryName").text();
intddlState = $(this).find("intStateID").text();
strddlState = $(this).find("intState").text();
intddlCity = $(this).find("intCityID").text();
strddlCity = $(this).find("strCityName").text();

$.ajax({
    url: getCountry(),
    async: true,
    success: function (response) {
        $("#selCountry").val(intddlCountry);
        $("#select2-selCountry-container").text(strddlCountry);

        $.ajax({
            url: GetState(),
            async: true,
            success: function (response) {
                $("#selState").val(intddlState);
                $("#select2-selState-container").text(strddlState);

                $.ajax({
                    url: getCity(),
                    success: function (response) {
                        $("#selCity").val(intddlCity);
                        $("#select2-selCity-container").text(strddlCity);
                    }
                });
            }
        });
    }
});  
});      
}