加载值后更改下拉列表的值

时间:2013-01-21 12:01:44

标签: c# jquery asp.net-mvc json

我需要在加载其中的所有值后设置dropdown值。第一个dropdown(区)值加载第二个dropdown(理事会)的值,JSon调用使ID选择理事会{{1}中的值但是它只能在完全加载后设置:

dropdown

编辑:

我还有一个与区域下拉相关联的级联:

$("#PostalCode").keyup(function () {
    loadPTPostalCode();
});

$("#PostalCodeExtension").keyup(function () {
    loadPTPostalCode();
});

function loadPTPostalCode()
{
    if ($("#PostalCode").val() >= 1000) {
       $.ajax({
            url: '/Address/GetPTPostalCode',
            type: "POST",
            dataType: "json",
            data: { postalCode: $("#PostalCode").val(), postalCodeExtension: $("#PostalCodeExtension").val() },
            success: function (data) {
                 //Set the Distict value and fire the load of the Councils Dropdown
                $("#districtDropdown").val(data.PTCouncil.PTDistrict.Id).change(function () {
                 // I tried to define the Council value after the load of his dropdown changes but no luck
                    $("#councilDropdown").val(data.PTCouncil.Id);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //alert(textStatus)
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

我不确定我完全理解你的问题,但试试这个:

 success: function (data) {
                 //Set the Distict value and fire the load of the Councils Dropdown
                $("#districtDropdown").val(data.PTCouncil.PTDistrict.Id).change(function () {
                 // I tried to define the Council value after the load of his dropdown changes but no luck
                    $("#councilDropdown").val(data.PTCouncil.Id);
                }).change();
            }

答案 1 :(得分:0)

只有在加载后才能在council下拉列表中设置值,这可能是通过另一个Ajax调用发生的。我假设你有另一个java脚本代码,它在区域值的变化上加载议会值(可能是区域下拉列表的更改处理程序)。所以你需要在该函数中设置council值。大纲将类似于

success: function (data) {
  //Set the Distict value
  $("#districtDropdown").val(data.PTCouncil.PTDistrict.Id);
  // store the council id in temp storage (currently using DOM element)
  $("#councilDropdown")[0]["temp_council_id"] = data.PTCouncil.Id;
  // fire loading of council drop-down
  $("#districtDropdown").change(); // this is assuming that change handler has the
                                   // relevant code. If not then invoke that code.
}

现在,在加载council下拉列表的代码中,添加代码以从临时存储设置议会ID

$("#districtDropdown").change(function() {
   // code to load council drop-down
   ....

   // set the selected council id (if any)
   var councilId = $("#councilDropdown")[0]["temp_council_id"];
   if (councilId) {
       $("#councilDropdown").val(councilId);
       $("#councilDropdown")[0]["temp_council_id"] = null; // clear temp storage
   }
});