2017-10-22 13 views
-1

でコンボボックスを作成し、私をheplください私はID]タブおよび配列値とのコンボボックスを作成したいが、私のコードは動作しませんID]タブおよび配列値

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script> 
    function createCombobox(tabId, arr) {   
     var select = document.getElementById("tabId");   
     for(var i = 0; i < arr.length; i++) { 
      var opt = arr[i]; 
      var el = document.createElement("option"); 
      el.textContent = opt; 
      el.value = opt; 
      select.appendChild(el);    
     } 
    } 
</script> 

<select id="checkWeek"> 
    <option>Choose a week</option> 
</select> 
<script> 
    $(document).ready(function(){ 
     var arr = [1, 2, 3, 4, 5, 6]; 
     createCombobox(checkWeek,arr); 
    }); 
</script> 

ありがとうございました。あなたはあなたのコード内の2つの問題の下に修正する必要があり

+1

へ、 本当に? :-( –

答えて

1

変更createCombobox(checkWeek,arr);変更var select = document.getElementById("tabId");

createCombobox("checkWeek",arr);から var select = document.getElementById(tabId);そのため `jQuery`をロード

$(function() { 
 
\t var arr = [1, 2, 3, 4, 5, 6]; 
 
\t createCombobox("checkWeek", arr); 
 
}); 
 

 
function createCombobox(tabId, arr) { 
 
\t var select = document.getElementById(tabId); 
 
\t for (var i = 0; i < arr.length; i++) { 
 
\t \t var opt = arr[i]; 
 
\t \t var el = document.createElement("option"); 
 
\t \t el.textContent = opt; 
 
\t \t el.value = opt; 
 
\t \t select.appendChild(el); 
 
\t } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<select id="checkWeek"> 
 
    <option>Choose a week</option> 
 
</select>

+0

ありがとうございました。 –

関連する問題