的onchange上的DropDownListonchange、DropDownList

2023-09-10 16:37:06 作者:浅夏初晴

我的问题是延续我有什么要求看到该链接。 加载国家/国家/城市

我已经扩大到从数据库加载我的下拉菜单列表,我只是需要一种方法来连接的onchange方法,在我的第一个DropDownList和第二,请参阅code。 AP preciate任何帮助。

最新追加code:的

 <选择一个id =国家的onchange =getStateByCountryId()>< /选择> < BR />
<选择一个id =状态>< /选择> < BR />


$(文件)。就绪(函数(){
     VAR的选择= {
         键入:POST,
         网址:SearchPage.aspx / LoadCountry
         数据: {},
         的contentType:应用/ JSON的;字符集= UTF-8,
         数据类型:JSON,

         成功:函数(MSG){

             VAR returnedArray = msg.d;
             国家= $(#国);
              country.append('<选项>选择一个国家< /选项>');

             对于(i = 0; I< returnedArray.length;我++){
                  country.append(<期权价值='+ returnedArray [I] .ID +'>中+ returnedArray [I] .Name点+< /选项>中);
             }


         }
     };
     $阿贾克斯(选件);
 });


传播getStateByCountryId(){

     $(#国)。改变(函数()
     {
         。VAR _selected = $(#国)VAL();
         VAR的选择=
         {
             键入:POST,
             网址:SearchPage.aspx / StateBy
             数据:{'countryId':'+ _selected +'},
             的contentType:应用/ JSON的;字符集= UTF-8,
             数据类型:JSON,

             成功:函数(MSG){
                $('#州)空();
                 VAR returnedArray = msg.d;


                 状态= $(#状态);
                 对于(VAR I = 0; I< returnedArray.length ++我){
                     state.append(<期权价值='+ returnedArray [I] .ID +'>中+ returnedArray [I] .Name点+< /选项>中);
                 }
             }
         };
         $阿贾克斯(选件);
     });
 }
 

但不填充?顺便我做的是,你怎么想办?

感谢。

解决方案

  $(#状态)。改变(函数(){
    //在变化code进入这里。
    //变量这个引用国家下拉元
});
 

my question is continuation of what i have asked see the link. Load Country/State/City

i have expand to load my drop downs list from db and i just need a way to wire onchange method in my first dropdownlist and second, please see the code. appreciate any help.

Append latest code:

<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);
     });
 }

but does not populate? the way i am doing is that how you suppose to do?

thanks.

解决方案

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