嗨我有一个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的案件工作?
答案 0 :(得分:7)
您的子字符串总是只有一个字符:
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;
}
}
}
答案 1 :(得分:1)
obj.id.substring(obj.id.length-1, obj.id.length);
此代码仅检索子字符串的最后1个字符,因此它不适用于两位数字。解决此问题的最简单方法是让它取最后两个字符,并更改您的选项&lt; 10,以便它们是“Info_01”,“Info_02”,“Info_03”等。
答案 2 :(得分:0)
你有一个子串保证返回1,只有1个字符。总是
var s = "sample";
s.substring(s.length-1,s.length);
将始终返回“e”。
修复你的子串,你会很好。
答案 3 :(得分:0)
试试这个正则表达式;它将在对象ID的末尾提取整个数字字符系列:
var indx = obj.id.match( /([0-9]+)$/ )[ 0 ];
更详细地说,您可以选中支票以确保匹配:
var m = obj.id.match( /([0-9]+)$/ );
if ( m.length == 0 )
return;
var indx = m[ 0 ];
答案 4 :(得分:0)
我知道这与上面针对switch语句的参数使用字符串类型变体的初始问题所述的情况不同,但我想在这里发布一个“gotcha”,因为它是沿着相同的行“javascript switch statement not working”。
我想要介绍的场景是当您尝试将数字类型变量用于switch语句的参数时。您可以遇到一个时髦的情况,即使您将变量转换为数字,它仍然不会注册为数字。
看看这个场景:
var intCase = document.getElementById("somePageElementWithANumericValue").value;
intCase = new Number(intCase); //This is supposed to be a number now...
switch(intCase)
{
case 0:
alert("Case 0");
break;
case 1:
alert("Case 1");
break;
case 2:
alert("Case 2");
break;
default:
//But this is what will execute
alert("Unexpected case? --> [" + intCase + "]");
break;
}
因此,针对这种情况的修复是通过强制它成为一个数字来获取转换后的变体并作弊。只需向其添加零,现在它显式为数字。
intCase = new Number(intCase) + 0; //This is like type casting
这是我最近遇到的情况,直到我做了“类型转换”技巧,我根本无法使用该脚本。我不明白为什么Number类没能完成它的工作,但至少有工作。
现在我确信有些人从未遇到过像这样的问题。我所能说的就是这对每个人来说都不是问题,但我知道这些奇怪的问题确实发生了。由于JavaScript是一种脚本语言,并且它没有强类型,我希望会发生这类问题。 VBScript并不是更好,我已经看到它做了一些真正令人难以置信的糟糕的事情,比如评估一个明显属于假的if语句。
答案 5 :(得分:-1)
问题是由于您使用了substring()。
答案 6 :(得分:-1)
使用:
var indx = obj.id.match( /\d+$/ )[0];
找到你的索引。 正则表达式匹配字符串末尾的一个或多个数字。