javascript - 使用 Meteor 创建提交按钮

标签 javascript html meteor

我想创建一个提交按钮,点击它会返回分数。但是,我收到错误 "getScore() is not defined.

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

这是 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>

最佳答案

尝试使用

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

代替

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

关于javascript - 使用 Meteor 创建提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41300385/

相关文章:

javascript - 试图包含一个简单的模块 - 无法让它工作纯 Javascript

javascript - 更新 div 内的计时器。 JavaScript

html - 本 map 片Base64转换报错Javascript

meteor - 将数据从 Iron 路由器传递到模板助手

javascript - 检查 Gmail 按钮的 javascript 调用

javascript - 是否可以在不超过调用堆栈的情况下展平一个 700 000 个条目的数组?

html - 如何将 CSS calc() 与元素的高度一起使用

javascript - 如何使用 JavaScript 将 child.jsp 转换为模式或覆盖?

javascript - 在 Meteor.js 中运行后台任务

javascript - Python处理如何与Meteor链接