2017-01-03 52 views
0

我试图获取每个选中的复选框的值为true。每次我选择一个复选框时,它会抓取该复选框的值而不是所有选中的复选框。获取每个选中的复选框的值

路径:talentList.html

<fieldset>   
    <div class="checkbox"> 
    <label> 
     <input name="specialisation" type="checkbox" value="Accounting Firm"> Accounting Firm 
    </label> 
    </div> 

    <div class="checkbox"> 
    <label> 
     <input name="specialisation" type="checkbox" value="Accounting in Industry"> Accounting in Industry 
    </label> 
    </div> 
</fieldset> 

路径:talentList.js

Template.talentList.events({ 
    'change [name="specialisation"]': function (event, template) { 
     let specialisation = event.target.value; 
     template.candidateListFilter.set(specialisation); 
    } 
}); 
+0

我不清楚你的问题是什么。您是否试图在选择复选框时获取所有选中的复选框值? – Khang

+0

是的,这是正确的 – bp123

回答

1

只有一个目标组在事件处理程序,以便将event.target.value是一个标量,而不是阵列。您需要遍历复选框数组。

Template.talentList.events({ 
    'change [name="specialisation"]': function (event, template) { 
    $.each($('[name="specialisation"]'),function(i,cb){ 
     let specialisation = cb.value; 
     template.candidateListFilter.set(specialisation); 
    }); 
    } 
}); 

说实话,这似乎是一个奇怪的模式。如果您想在选中/取消选中某个复选框时更新文档,则不必同时设置所有其他复选框的状态,您应该可以只戳一个您想要的。

+1

添加“[name ='specialization']:选中”,所以它只抓取选中的复选框。 – bp123

0

不知道这是正确的。它创建了所有选定选项的对象。

'change [name="specialisation"]': function (event, template) { 

    $(document).ready(function(){ 

     var specialisation = $('input[name="specialisation"]:checked').map(function(){ 

      return $(this).val(); 

     }); 

     var specialisationListArray = specialisation.get(); 
     template.candidateListFilter.set(specialisationListArray); 
    }); 
}, 
+0

在流星你不应该使用'$(document).ready()' –