简体   繁体   English

流星:如何根据部分中复选框的数量设置数字

[英]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. 这是一些代码。

Event Handler 事件处理程序

'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. 这里的目标是设置选定玩家的数量,并将其传递给模板/帮助程序。

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. 我只是不知道需要在何处设置变量以提供默认值。

Template 模板

<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.

相关问题 根据选中的复选框数更改表单的文本字段 - Change text field of form based on number of check boxes checked 根据使用CSS或JS选中的复选框数隐藏div - Hide divs based on number of check boxes checked with CSS or JS JavaScript:根据唯一的CLASS限制选中的允许框的数量 - JavaScript: Limit number of checked allowed boxes based on unique CLASS 如果选中了一定数量的框,如何显示图像 - How do I get an image to display if a certain number of boxes are checked 使用JavaScript检查和控制已选中复选框的数量 - Check and control the number of checked check boxes with JavaScript 限制允许选中的复选框数量 - Limit the number of check-boxes allowed to be checked 我们如何使用JavaScript计算在asp.net的网格视图内选中的复选框的数量? - How can we count the number of check boxes checked inside a grid view in asp.net using javascript? 编写jQuery代码以检查是否选中了复选框,并指出选中的复选框数量? - Write jquery code to check if the check boxes are checked or not and indicate the number of check boxes checked? 如何根据选中的复选框数量显示/隐藏元素? - How to show/hide elements based on number of checkbox checked? 复选框:当有多个选中的复选框时,启用/禁用未选中的复选框 - Check box: Enable/Disable unchecked boxes when there is a number of checked ones
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM