所以我正在构建一个带有把手的模板,我想将const totalSum附加到const sumTextSidebar。但显然它无法找到它尚未创建它。这有解决方法吗?我在下面提供了我的代码。谢谢!
function multi(element) {
const responseSidebar = ($(element).data('sidebar-respond'));
const responseUnder = ($(element).data('response-under'));
const optionId = Number($(element).data('optionid')) + 1;
const questionId = $(element).data('questionid') + 1;
const responseUnderArea = '#quiz-alert-' + questionId;
const sidebarId = '#sidebar-' + (questionId - 1) + '-' + (optionId - 1);
const sidebarQuestion = '#sidebar-' + (questionId - 1);
const responseUnderID = "#" + (questionId - 1) + "-" + (optionId - 1);
const sumTextUnderQuestionID = '#sumTextUnder-' + (questionId - 1);
**const sumTextSidebar** = ($(element).data('sumTextSidebar')+ totalSum);
const sumSidebarElement = $('<li>' + sumTextSidebar + '</li>');
const values = Number($(element).val());
const sumSidebarId = '#sumTextID-' + questionId;
if ($(element).is(':checked')) {
$(sidebarId).removeClass('hidden');
$(responseUnderID).removeClass('hidden');
$(responseUnderArea).removeClass('hidden');
$(sumTextUnderQuestionID).removeClass('hidden');
if ($(sumSidebarId).length == 0) {
$(sidebarQuestion).append(sumSidebarElement).slideDown('slow');
sumSidebarElement.attr('id', 'sumTextID-' + questionId);
}
addValue.push(values)
} else {
$(sidebarId).addClass('hidden');
$(responseUnderID).addClass('hidden');
subtractValue.push(values)
}
function getSum(total, num) {
return total + num;
}
const addValueSum = addValue.reduce(getSum);
const subtractValueSum = subtractValue.reduce(getSum);
**const totalSum** = addValueSum - subtractValueSum;