2010-04-19 113 views
19

我的问题是我所要求的连续查看链接。 Load Country/State/Cityonchange下拉列表

我已经扩展到从我的数据库加载我的下拉列表,我只需要一种方法在我的第一个dropdownlist中连接onchange方法,第二,请参阅代码。感谢任何帮助。

追加最新的代码:

<select id="country" onchange="getStateByCountryId()"></select> <br /> 
<select id="state"></select> <br /> 


$(document).ready(function() { 
    var options = { 
     type: "POST", 
     url: "SearchPage.aspx/LoadCountry", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 

     success: function(msg) { 

      var returnedArray = msg.d; 
      country = $("#country"); 
       country.append('<option>Select a Country</option>'); 

      for (i = 0; i < returnedArray.length; i++) { 
        country.append("<option value='" + returnedArray[i].Id + "'>" + returnedArray[i].Name + "</option>"); 
      } 


     } 
    }; 
    $.ajax(options); 
}); 


function getStateByCountryId() { 

    $("#country").change(function() 
    { 
     var _selected = $("#country").val(); 
     var options = 
     { 
      type: "POST", 
      url: "SearchPage.aspx/StateBy", 
      data: "{'countryId':'" + _selected + "'}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 

      success: function(msg) { 
       $('#state').empty(); 
       var returnedArray = msg.d; 


       state = $("#state"); 
       for (var i = 0; i < returnedArray.length; ++i) { 
        state.append("<option value='" + returnedArray[i].Id + "'>" + returnedArray[i].Name + "</option>"); 
       } 
      } 
     }; 
     $.ajax(options); 
    }); 
} 

但不填充?我正在做的方式是你想要做什么?

谢谢。

回答

25
$("#state").change(function(){ 
    //on-change code goes in here. 
    //variable "this" references the state dropdown element 
}); 
+0

我如何得到父母(国家)下拉列表元素的id? – 2010-04-19 15:31:50

+1

“country”不是上述HTML中的“父”,它只是另一个元素。上面的代码,但用#country替换'#state'会得到你想要的。或者我误解了?你想要“前一个”元素吗? – Graza 2010-04-19 15:42:09

3
$(this).parent("select").attr("id"); 
+0

其中