0

我只希望我的 jQuery 函数只运行一次。Currentyl 我正在使用国家,州/引用下拉列表。因此,当用户选择任何国家时,它会显示城市/州。但最初它什么也没显示,所以我在 document.ready 上调用它。但问题是每次都显示第一个国家城市。那么有没有解决方案。或者你能推荐任何其他国家,城市下拉。

下拉代码是

function print_country(country_id){
// given the id of the <select> tag as function argument, it inserts <option> tags
var option_str = document.getElementById(country_id);
var x, i=0;
for(x in country_arr){
    option_str.options[i++] = new Option(country_arr[x],country_arr[x]);
}
}

function print_state(state_id, state_index){
var option_str = document.getElementById(state_id);
var x, i=0; state_index++;
var state_arr = s_a[state_index].split("|");
for(x in state_arr){
        option_str.options[i++] = new Option(state_arr[x],state_arr[x]);
}
}

您还可以对其进行修改,以便在页面加载默认情况下显示第一个国家/地区的引用。谢谢

4

1 回答 1

0

现在你应该使用 jQuery

function print_country(country_id){
  // given the id of the <select> tag as function argument, it inserts <option> tags
  var countrySel = $("#"+country_id);
  // create a "singleton" by returning if the state exists
  if (countrySel.size() > 1) return;
  $.each(country_arr,function() {
      countrySel.append('<option value="'+$(this)+'">'+$(this)+'</option>');
  });
  // call it with the second option (assuming "Please select")
  // change to ,0 for first option
  print_state("state", 1); 
}

function print_state(state_id, state_index){
  var stateSel = $("#"+state_id);
  var state_arr = s_a[state_index].split("|");
  $.each(state_arr,function() {
    stateSel.append('<option value="'+$(this)+'">'+$(this)+'</option>');
  });
}
于 2013-04-29T13:15:13.130 回答