我正在创建一个场景,其中多个问题需要 3 个单选按钮。我向单选按钮添加了值,我想将所选单选按钮的总分相加并将其显示到文本框中。
这是我的代码,我很难弄清楚为什么我收到未定义的错误消息。
function getSelectionPoints(selection) {
return parseInt(selection.split(",")[1]);
}
function getSelectionObj(selectionID) {
var selection = null;
if(selectionID == 1)
selection = document.getElementById("<%=selection1.ClientID %>").getElementsByTagName("input");
else if (selectionID == 2)
selection = document.getElementById("<%=selection2.ClientID %>").getElementsByTagName("input");
else if (selectionID == 3)
selection = document.getElementById("<%=selection3.ClientID %>").getElementsByTagName("input");
return selection;
}
function getTotalPoints() {
var totalPoints = 0;
var selection;
var promotionDropdown = document.getElementById("<%=dropDownPromotionTo.ClientID %>");
var promotionFromID = parseInt(document.getElementById("<%=promotionIDFromHidden.ClientID %>").value);
var promotionToID = parseInt(promotionDropdown.options[promotionDropdown.selectedIndex].value);
for (var i = 1; i <= 9; i++) {
selection = getSelectionObj(i);
if (i == 9 && (promotionFromID == 2 || promotionFromID == 3 || promotionToID == 2 || promotionToID == 3)) {
totalPoints += 0;
}
else if (selection != null) {
for (var j = 0; j < selection.length; j++) {
if (selection[j].checked) {
totalPoints += getSelectionPoints(selection[j].value);
break;
}
}
}
}
alert(totalpoints);
return totalPoints;
}
<asp:RadioButtonList ID="selection1" runat="server"
EnableTheming="True" SkinID="Black" CausesValidation="True" onchange="updateMyPoints(false, true);">
<asp:ListItem Value="1,6">Spectacular</asp:ListItem>
<asp:ListItem Value="2,4" Selected="True">Average</asp:ListItem>
<asp:ListItem Value="3,0">Needs improvement</asp:ListItem>
</asp:RadioButtonList>
最佳答案
JavaScript 区分大小写,请使用 totalPoints
而不是 totalpoints
:
alert(totalPoints);
关于javascript - 为什么这是未定义的以及如何解决这个问题? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31391638/