【问题标题】:How to select Multi Check Boxes in HTML using Meteor?如何使用 Meteor 在 HTML 中选择多个复选框?
【发布时间】:2014-05-21 16:15:47
【问题描述】:

我需要了解如何使用流星在 html 中选择多个复选框。我做了一个示例。在此示例中选择多个复选框。如何获取所选数据以及如何将数据存储在数组中。请问查看下面的代码并建议我该怎么做?

如果arrayname = Bike,car,Computer(这些是选定项),则存储在数组中的详细信息

HTML 代码:

<form id="cb-form" action="action">
<input type="checkbox" name="owns" value="Bike">Bike<br/>
<input type="checkbox" name="owns" value="Car">car<br/>
<input type="checkbox" name="owns" value="Refridgerator">Refridgerator<br/>
<input type="checkbox" name="owns" value="Mobile">Mobile<br/>
<input type="checkbox" name="owns" value="Tablet">Tablet<br/>
<input type="checkbox" name="owns" value="Computer">Computer<br/>
<input type="submit"  value="send" />
</form>

JS代码:

Template.login.events

  ({

    'submit #cb-form' : function (e,t)

    {

      // template data, if any, is available in 'this'

      if (typeof console !== 'undefined')

        console.log("You pressed LOGIN Button");

        e.preventDefault();

       //retrieve the input field values

         //here write get multiple check boxes data logic same as above scenario
     }

  });

【问题讨论】:

    标签: javascript html meteor


    【解决方案1】:

    HTML:

    <template name="login">
      <form id="cb-form" action="action">
       <input type="checkbox" name="owns" value="Bike">Bike<br/>
       <input type="checkbox" name="owns" value="Car">car<br/>
       <input type="checkbox" name="owns" value="Refridgerator">Refridgerator<br/>
       <input type="checkbox" name="owns" value="Mobile">Mobile<br/>
       <input type="checkbox" name="owns" value="Tablet">Tablet<br/>
       <input type="checkbox" name="owns" value="Computer">Computer<br/>
       <input type="submit"  value="send" />
     </form>  
    </template>
    

    JS:

    Template.login.events({
     'submit #cb-form' : function (event, template) {
       event.preventDefault();
    
       var selected = template.findAll( "input[type=checkbox]:checked");
    
       var array = _.map(selected, function(item) {
         return item.defaultValue;
       });
    
       console.log(array);
    
     }
    });
    

    如果选择了 Car 和 Refridge,则输出 ["Car", "Refridgerator"]。它只是简单地使用下划线。查看下划线文档以供进一步阅读。

    【讨论】:

    • 我知道这是旧的,但如果你没有安装下划线,你可以使用 Array.map。以上将是 var array = selected.map(function(item){ return item.value})
    猜你喜欢
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 2017-05-22
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多