简体   繁体   中英

Popluating country and state using jquery and json in javascript

Hi I am trying to populate country and state dropdown list in dropdowns using jquery.

My json looks like this

Blockquote

var myjson = [{"countryName":"Afghanistan","countryCode":"AF"},{"countryName":"United States","countryCode":"US" ,"state":["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]},
{"countryName":"United States Minor Outlying Islands","countryCode":"UM"},
{"countryName":"Uruguay","countryCode":"UY","state":["abc","edf"]}];

My html

<body>
<select id = "country" name = "country" onchange= "getvalue()"></select>
<select id = "state" name = "state" onchage="getstate()"></select>
</body>

My javascript

<script>
function getvalue(){

 var country = document.getElementById("country").value;
 var divtemp = document.getElementById("test");
  for (var i = 0; i < myjson.length; i++ ) {


  if(myjson[i].state != null )
  {
  if(myjson[i].state.length==0){
  $("#state").empty();
    }        
  else
  {
    for(var j = 0 ; j< myjson[i].state.length;j++)
    {

        if(country === myjson[i].countryName)
        {
        //alert(country);
        //alert(myjson[i].countryName);
        //divtemp.innerHTML= myjson[i].state[j] + "<br>"+divtemp.innerHTML;
        $("#state").append(

            $("<option></option>")

                .text(myjson[i].state[j])

        );
        }
    }

  }
 }
 }
 </script>

This script function is populating states but when I select a another country the sates are appending to the earlier states list. I am not able to make out the changes required for my condition. I dont want to use Ajax to do this.

Try this. In the head do this...

<script>
$(document).ready(
  //when the document is loaded, initialize my selectors
  function initCountries(){
     //put all countries into the country selector
     for (cix in myjson){
       var options='<option value="'+cix+'">'+myjson[cix].countryName+'</option>';
       $("#country").append(options);
  }
  //listen for the change event when the user chooses a country
  $("#country").change(function fillStates(){
    //find the country the user chose, then fill the states selector
    var cix=$(this).val();
    var states=myjson[cix].state;
    $("#state").empty();
    for (six in states){
        var options='<option value="'+states[six]+'">'+states[six]+'</option>';
        $("#state").append(options);
    }
  });
  //listen for the change event when the user chooses a state
  $("#state").change(function stateSelected(){
    //show the state the user chose
    var six=$(this).val();
    alert(six);
  });

});
</script>

and in your body change the selectors like this...

<select id = "country"></select>
<select id = "state"></select>

Clear state combobox on change of country..

document.getElementById('state').innerHTML = "";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM