1

dropdown加载其中的所有值后,我需要设置一个值。第一个dropdown(District) 值加载第二个dropdown(Councils) 的值,并JSon调用带来ID选择 Councils 中的值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)
            }
        });
    }
}

编辑:

我还有一个与 District Dropdown 相关的级联:

$("#districtDropdown").cascade({
    url: "/Address/ListCouncilByDistrict",
    paramName: "districtId",
    firstOption: 'Selecione o Concelho...',
    childSelect: $("#councilDropdown")
});
4

2 回答 2

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();
            }
于 2013-01-21T12:03:12.437 回答
0

只有在加载后才能在委员会下拉列表中设置值,这可能是通过另一个 Ajax 调用发生的。我假设您有另一个 java 脚本代码,它在改变地区值时加载理事会值(可能在地区下拉列表的更改处理程序中)。因此,您需要在该函数中设置委员会值。大纲将类似于

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.
}

现在,在加载理事会下拉列表的代码中,添加代码以从临时存储中设置理事会 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
   }
});
于 2013-01-21T12:15:51.880 回答