我想创建一个提交按钮,点击后会返回分数。但是,我收到错误"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>
答案 0 :(得分:0)
尝试使用
UserResult.update({ _id: selectedUser }, {**$set**:{ score: 5 }});
而不是
UserResult.update({ _id: selectedUser }, { score: 5 });
答案 1 :(得分:0)
我发现没有人回答这个问题,所以我使用了autoform包。让生活更轻松。