2016-03-16 144 views
0

所以我想創建一定數量的基於「timesPerWeek」值的複選框。我遇到的問題是,當嘗試createElement時,我無法讀取null的屬性'createElement'。很確定,這是因爲JS呈現在HTML之前,但我無法弄清楚需要做些什麼才能使它可以調用我的函數來製作複選框。此代碼目前不依賴於timesPerweek,因爲我只是想讓複選框先顯示出來。流星創建動態複選框

JS

'pie':function() 
    { 
    var progress = document.getElementById('progress'); 

     var stuff= progress.createElement('input'); 
     //var stuff = progress.createElement('input'); 
     stuff.type = "checkbox"; 
     stuff.name = "1"; 
     stuff.value = "set1"; 
     stuff.id = "stuff"; 

     var label = progress.createElement('label'); 
     label.htmlFor = "stuff"; 
     label.appendChild(document.createTextNode('pie')); 

     progress.appendChild(stuff); 
     progress.appendChild(label); 

    } 

HTML

<div id="progress" class = "col-md-4"> 
       progress: {{timesPerWeek}} 

       <br/> 

       {{pie}} 


       </div> 

我應該能夠創造條件,進步的元素,但它只是與上述錯誤死亡。

回答

0

的正確方法流星做到這一點是使用#each幫手:

<div id="progress"> 
    {{#each timesPerWeek}} 
    <input type="checkbox" name="checkbox{{@index}}"> 
    {{/each}} 
</div> 

這應該讓你幾個複選框露面。

+0

偉大的工程唯一的問題是時代週刊只是一個數字,所以我做了一個函數,返回一個數組 –

+0

陷阱。很高興工作! –