2011-07-22 23 views
0

を更新する私は2選択ボックス選択ボックスのオプションに

  1. Countrynames
  2. 空港

そして、私は最初の選択ボックスでCOUNTRYNAMEを選択するとAjaxリクエストが送信されますと、それはようAirpostsのリストを返すがあります

のようになりました。選択タグのオプションのみを置き換える必要があります。 私は

var country_select = document.getElementById("country_select"); 
country_select.options.length = 0; 
country_select.options = response.responseText 

ような何かをしようとしていますが、この割り当ては、それが私を成し遂げることがどのように機能していません!

+0

は、応答htmlまたはjsonですか?それを使うには何かをしなければならないかもしれないからです。 – nemesisfixx

答えて

0

これはもう少しトリッキーです

var country_select = document.getElementById("country_select"); 
country_select.innerHTML = response.responseText; 
0

を試してみてください、あなたはinnerHTMLのか、そのようなものを使用してそれを行うことができなくなります。 あなたのajaxが空港名を返す方法を変更する必要があります。 <option>abc</option><option>xyz</option>の代わりに、例えば||で区切られた名前の文字列を返します。 :abc||def||xyz。文字列をJSの配列に分解し、arrayの各要素からオプション要素を作成し、ドロップダウンに追加します。次の点を確認してください:

<html> 
<head> 
    <script> 
     var ajaxResponse = "abs||def||xyz||test||blah"; 

     function updateOptions(optionsString) 
     { 
      var options = optionsString.split("||"); 

      for(var i=0; i< options.length; i++) 
      { 
       AddItem(options[i], options[i]); 
      } 
     } 

     function AddItem(Text,Value) 
     { 
      var opt = document.createElement("option"); 
      opt.text = Text; 
      opt.value = Value; 

      document.getElementById("mydropdown").options.add(opt); 
     } 

     function removeOptions() 
     { 
      document.getElementById('mydropdown').length = 0; 
     } 
    </script> 
</head> 
<body> 
    <input type="button" onclick="updateOptions(ajaxResponse)" value="update"></input> 
    <input type="button" onclick="removeOptions()" value="remove"></input> 
    <select id="mydropdown"> 
    </select> 
</body> 
</html> 
関連する問題