[英]Meteor: How to set a number based on the number of checked boxes within a section
I currently have a multi-section form, with a number of checkboxes. 我目前有一个包含多个复选框的多节形式。 What Im trying to do, is show the number of checked boxes, next to the total.
我想做的是在总数量旁边显示复选框的数量。 I have total showing up just fine, but I cant for the life of me figure out a way to loop over the inputs, successfully find the :checked, and print that number out.
我的总显示效果很好,但是我无法终生想出一种方法来遍历输入,成功找到:checked并打印出该数字。
I think the main thing causing me issues, is that it needs to update every time a new box is checked. 我认为导致我出现问题的主要原因是,每次选中新框时,它都需要更新。 Heres some of the code.
这是一些代码。
'click input[type=checkbox]': function(){
Session.set('selectedPlayerCount', n);
Session.get('selectedPlayerCount');
}
The goal here is to set the number of selected players, and pass it to the template/helper.
这里的目标是设置选定玩家的数量,并将其传递给模板/帮助程序。
countSelected: function(){
n = 0;
n++;
var selectedPlayerCount = Session.get('selectedPlayer');
return selectedPlayerCount;
}
Within the helper I'm attempting to iterate every time the event is triggered, and as a result increase the value by one.
在助手中,每次事件触发时我都尝试进行迭代,因此将值增加一。 I'm aware that resetting n back to 0 is going to cause some issues, and I know that needs to be changed one the rest is figured out.
我知道将n重置为0会导致一些问题,并且我知道需要将其更改为其他值。 I just cant figure out where the variable needs to be set in order to provide a default value.
我只是不知道需要在何处设置变量以提供默认值。
<header>
<p>{{ countSelected }}</p>
</header>
All I'm trying to do here for now is to print out the value rendered by the helper.
我现在在这里要做的只是打印出助手所提供的值。 I don't believe this is causing any issues.
我认为这不会引起任何问题。
TL;DR - How to count number of checked inputs within a section of a form, and for each one, increment a value, and then return it every time its changed. TL; DR-如何计算表单的某一部分内已检查输入的数量,并为每个输入增加一个值,然后在每次更改时将其返回。
I'm new to this but maybe this will serve: define your account to zero in a session variable, each time you select a checkbox you increase the value of your session variable 我是新手,但也许可以这样做:在会话变量中将帐户定义为零,每次选择复选框时,您都会增加会话变量的值
Session.set("countPlayersChecked", 0);
Template.proof.helpers({
countSelected: function () {
return Session.get("countPlayersChecked");
}
});
Template.proof.events({
'click input[type=checkbox]': function (event) {
if($(event.target).is(':checked'))
Session.set("countPlayersChecked", Session.get("countPlayersChecked")+1);
else
Session.set("countPlayersChecked", Session.get("countPlayersChecked")-1);
}
});
Hope it serves. 希望它有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.