gpt4 book ai didi

javascript - jquery sum 不起作用,调用函数时总和变为零

转载 作者:行者123 更新时间:2023-11-30 20:23:47 25 4
gpt4 key购买 nike

我正在为考试创建一个小应用程序,当我点击下一步按钮时,当前的答案单选按钮值应该存储在某个变量中并且该值应该添加到下一个答案值中,我正在用jquery,但是当我多次调用该函数时,我的存储值总是重新设置为零,

这是我到目前为止尝试过的。以下是我的查看页面

<div>

<div id="questionvalue">@a.QDescription</div>
<div>
<div id="questionvalue">
<input type="radio" name="test" value="@a.Choice1Percent"> @a.Choice1Desc<br>
</div>

</div>
<div>
<input type="radio" name="test" value="@a.Choice2Percent"> @a.Choice2Desc<br>

</div>
<div>
<input type="radio" name="test" value="@a.Choice3Percent"> @a.Choice3Desc<br>

</div>
<div>
<input type="radio" name="test" value="@a.Choice4Percent"> @a.Choice4Desc<br>

</div>

<button type="button" id="starttestbuttonid" class="btn btn-default" onclick="StartTest()">Next</button>

以下是我尝试添加答案总和的脚本

<script>
function StartTest() {
debugger;
var selopt = parseFloat($('input[name=test]:checked').val());

var optionssum=0;
optionssum+= optionssum + selopt;

$.ajax({
url: $("baseUrl").html() + "Test/_QuestionView",
type: "POST",
data: { selectedoption: selopt},
success: function (data) {
debugger;

$('#appendquestion').html(data);
// $('#myModal').modal(show);



},
error: function (data) {
$('#loading').hide();
}
});
}

任何人都可以帮助计算总和。

最佳答案

你不能做 optionsum += optionsum + selopt; 我想你的意思是 optionsum += selopt;

当然,optionsum 每次都会被重置,你可以在 var optionsum=0; 里面 StartTest() 和每次按钮被点击即被调用。

只要把你的代码改成这样,它应该可以工作(让它成为一个全局变量)

var optionssum=0;    
function StartTest() {
debugger;
var selopt = parseFloat($('input[name=test]:checked').val());


optionssum+= selopt;

$.ajax({
url: $("baseUrl").html() + "Test/_QuestionView",
type: "POST",
data: { selectedoption: selopt},
success: function (data) {
debugger;

$('#appendquestion').html(data);
// $('#myModal').modal(show);



},
error: function (data) {
$('#loading').hide();
}
});
}

关于javascript - jquery sum 不起作用,调用函数时总和变为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51147757/

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