gpt4 book ai didi

javascript switch语句不起作用

转载 作者:行者123 更新时间:2023-11-30 08:14:59 26 4
gpt4 key购买 nike

你好,我有一个 switch 语句,它在情况 9 或更少的情况下工作正常,如下所示

    function checkBoxes(obj) {
var indx = obj.id.substring(obj.id.length-1, obj.id.length);
switch ( indx ) {
case '1':

if (document.sportsInfo.Info_1.checked) {

document.sportsInfo.Info_2.disabled = true;
document.sportsInfo.Info_2.checked = false;
document.sportsInfo.Info_3.disabled = true;
document.sportsInfo.Info_3.checked = false;
document.sportsInfo.Info_4.disabled = true;
document.sportsInfo.Info_4.checked = false;
document.sportsInfo.Info_5.disabled = true;
document.sportsInfo.Info_5.checked = false;
document.sportsInfo.Info_6.disabled = true;
document.sportsInfo.Info_6.checked = false;
document.sportsInfo.Info_7.disabled = true;
document.sportsInfo.Info_7.checked = false;
document.sportsInfo.Info_8.disabled = true;
document.sportsInfo.Info_8.checked = false;
document.sportsInfo.Info_9.disabled = true;
document.sportsInfo.Info_9.checked = false;
document.sportsInfo.Info_10.disabled = true;
document.sportsInfo.Info_10.checked = false;
document.sportsInfo.Info_11.disabled = true;
document.sportsInfo.Info_11.checked = false;
document.sportsInfo.Info_12.disabled = true;
document.sportsInfo.Info_12.checked = false;
}
else {
document.sportsInfo.Info_2.disabled = false;
document.sportsInfo.Info_3.disabled = false;
document.sportsInfo.Info_4.disabled = false;
document.sportsInfo.Info_5.disabled = false;
document.sportsInfo.Info_6.disabled = false;
document.sportsInfo.Info_7.disabled = false;
document.sportsInfo.Info_8.disabled = false;
document.sportsInfo.Info_9.disabled = false;
document.sportsInfo.Info_10.disabled = false;
document.sportsInfo.Info_11.disabled = false;
document.sportsInfo.Info_12.disabled = false;
}
break;

但是当它达到情况 10 或以上时,它根本不起作用:

    case '10':
if (document.sportsInfo.Info_10.checked) {
document.sportsInfo.Info_1.disabled = true;
document.sportsInfo.Info_1.checked = false;
document.sportsInfo.Info_2.disabled = true;
document.sportsInfo.Info_2.checked = false;
document.sportsInfo.Info_3.disabled = true;
document.sportsInfo.Info_3.checked = false;
document.sportsInfo.Info_4.disabled = true;
document.sportsInfo.Info_4.checked = false;
document.sportsInfo.Info_5.disabled = true;
document.sportsInfo.Info_5.checked = false;
document.sportsInfo.Info_6.disabled = true;
document.sportsInfo.Info_6.checked = false;
document.sportsInfo.Info_7.disabled = true;
document.sportsInfo.Info_7.checked = false;

document.sportsInfo.Info_8.disabled = true;
document.sportsInfo.Info_8.checked = false;
document.sportsInfo.Info_9.disabled = true;
document.sportsInfo.Info_9.checked = false;

}
else {
document.sportsInfo.Info_1.disabled = false;
document.sportsInfo.Info_2.disabled = false;
document.sportsInfo.Info_3.disabled = false;
document.sportsInfo.Info_4.disabled = false;
document.sportsInfo.Info_5.disabled = false;
document.sportsInfo.Info_6.disabled = false;
document.sportsInfo.Info_7.disabled = false;

document.sportsInfo.Info_8.disabled = false;
document.sportsInfo.Info_9.disabled = false;

}
break;

如何让大于或等于 10 的案例起作用?

最佳答案

你的子字符串总是只有一个字符长:

substring(obj.id.length-1, obj.id.length)

顺便说一句:如果你想禁用除已选中的复选框之外的所有其他复选框,你可以这样做:

function checkBoxes(elem) {
if (elem.checked) {
for (var i=1; i<=12; i++) {
if ("Info_"+i == elem.id) continue;
document.sportsInfo["Info_"+i].disabled = true;
document.sportsInfo["Info_"+i].checked = false;
}
} else {
for (var i=1; i<=12; i++) {
if ("Info_"+i == elem.id) continue;
document.sportsInfo["Info_"+i].disabled = false;
}
}
}

关于javascript switch语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5252448/

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