こんにちは、jquery を使用してドロップダウンに国と州のドロップダウン リストを入力しようとしています。
私のjsonは次のようになります
引用符
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"]}];
私のhtml
<body>
<select id = "country" name = "country" onchange= "getvalue()"></select>
<select id = "state" name = "state" onchage="getstate()"></select>
</body>
私の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>
このスクリプト関数は州を設定していますが、別の国を選択すると、州が以前の州のリストに追加されます。自分の状態に必要な変更を行うことができません。これを行うためにAjaxを使用したくありません。