【问题标题】:Creating a submit button with Meteor使用 Meteor 创建提交按钮
【发布时间】:2017-05-09 02:19:31
【问题描述】:

我想创建一个提交按钮,点击后会返回分数。但是,我收到错误 "getScore() 未定义。

我也想将结果更新到数据库,但不确定我是否做得对。

这里是 Javascript

Meteor.user = function () {
  return Accounts.user();
};

if (Meteor.isClient) {


  Template.personality.events({
   'click :submit': function (event, template) {
     var getCheckedValue = template.find('input:radio[name]:checked');
     console.log($(getCheckedValue).val()),



     function getScore(){
        var answers = ["1","2","3","4","5"], 
        tot = answers.length;
        var score = 0;
        for (var i=0; i<tot; i++)
            if(getCheckedValue[i] ===answers[i]) { 
         score += 1; // increment only
        return score;
           }
        },              


       alert("Your score is "+getScore()+ "/"+ tot);

    // update the score to database
            var selectedUser = Session.get('this.userId');
            UserResult.update({ _id: selectedUser }, { score: 5 });
   //          }

   }
 });
}

HTML

<template name="personality">
<h1>I see myself as someone who...</h1>

  <form>
   <ul>
    <p>...is talkative.</p>
      <li>
        <input type = "radio" name = "Q1" value = "1">1
        <input type = "radio" name = "Q1" value = "2">2
        <input type = "radio" name = "Q1" value = "3">3
        <input type = "radio" name = "Q1" value = "4">4
        <input type = "radio" name = "Q1" value = "5">5
      </li>
      <p>...Tends to find fault with others.</p>

      <li> 
        <input type = "radio" name = "Q2" value = "1">1
        <input type = "radio" name = "Q2" value = "2">2
        <input type = "radio" name = "Q2" value = "3">3
        <input type = "radio" name = "Q2" value = "4">4
        <input type = "radio" name = "Q2" value = "5">5
      </li>
         <input type = "submit" value = "SUBMIT"/>      

      </ul>
     </form>


</template>

【问题讨论】:

    标签: javascript html meteor


    【解决方案1】:

    尝试使用

    UserResult.update({ _id: selectedUser }, {**$set**:{ score: 5 }});
    

    而不是

    UserResult.update({ _id: selectedUser }, { score: 5 });
    

    【讨论】:

    • 感谢您的回答,如果您也看看 getScore() 函数会很棒
    【解决方案2】:

    我发现没有人回答这个问题,所以我使用了 autoform 包。让生活更轻松。

    http://autoform.meteorapp.com/quickform

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-15
      • 2018-05-24
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多