【发布时间】: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