gpt4 book ai didi

javascript - 使用 Meteor 创建提交按钮

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:19 26 4
gpt4 key购买 nike

我想创建一个提交按钮,点击它会返回分数。但是,我收到错误 "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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com