javascript switch 语句不起作用
您好,我有一个 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 工作?
Hi I have a switch statement and it is working fine when it is case 9 or less like below
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;
but when it gets to case 10 or above it isn't working at all:
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;
how can I get cases greater to or equal to 10 to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个正则表达式;它将提取对象 ID 末尾的整个数字字符系列:
更详细地说,您可以进行检查以确保存在匹配:
Try this regular expression; it will extract the entire series of digit characters at the end of the object ID:
More verbosely, you could toss in a check to make sure there was a match:
我知道这与上面所说的初始问题不同,该问题使用字符串类型变体作为 switch 语句的参数,但我想在这里发布一个“陷阱”,因为它与“javascript”相同switch 语句不起作用”。
我想要介绍的场景是当您尝试使用数字类型变体作为 switch 语句的参数时。您可能会遇到一种奇怪的情况,即使您将变体转换为数字,它仍然不会注册为数字。
看一下这个场景:
因此,解决这种情况的方法是采用转换后的变体并通过强制其为数字来进行一点欺骗。只需添加零即可,现在它明确是一个数字。
这是我最近遇到的情况,直到我执行“类型转换”技巧之前,我根本无法让该脚本工作。我不明白为什么 Number 类未能完成其工作,但至少有解决办法。
现在我确信有人从未遇到过这样的问题。我只能说这可能不是每个人的问题,但我知道这些奇怪的问题确实会发生。由于 JavaScript 是一种脚本语言并且它不是强类型的,所以我预计会发生此类问题。 VBScript 也好不到哪里去,我见过它做了一些非常非常令人难以置信的坏事,例如评估一个显然是 true 与 false 的 if 语句。
I know this is a different case from what was stated above for the initial problem which is using a string type variant for the argument of switch statement, but I wanted to post a "gotcha" here since it is along the same lines of "javascript switch statement not working".
The scenario I want to cover is when you are trying to use a number type variant for the argument of the switch statement. You can run into a funky situation where even though you converted a variant to a Number it still will not register as a Number.
Look at this scenario:
So the fix for this situation is to take your converted variant and cheat a little by forcing it to be a number. Simply add zero to it, now it is explicitly a number.
This is a situation I ran into recently and until I did my "type casting" trick I couldn't get that script to work at all. I don't understand why the Number class failed to do its job, but at least there is the work around.
Now I am sure there are people who have never had a problem like this. All I can say about that is this might not be an issue for everyone, but I know these kinds of strange issues do happen. Since JavaScript is a scripting language and it is not strongly typed I would expect these kinds of problems to happen. VBScript is no better, i've seen it do some really really unbelievably bad things, such as evaluating an if statement that was clearly true to false.
该问题是由于您使用了 substring() 造成的。
The issue is due to your use of substring().
使用:
查找您的索引。
正则表达式匹配字符串末尾的一个或多个数字。
use:
to find your index.
The regular expression matches one or more digits at the end of a string.
您的子字符串始终只有一个字符长:
顺便说一下:如果您想禁用除选中复选框之外的所有其他复选框,您可以执行以下操作:
Your substring is always just one character long:
By the way: If you want to disable all other checkboxes than the checked one, you can do this:
obj.id.substring(obj.id.length-1, obj.id.length);
此代码仅检索子字符串的最后 1 个字符,因此不适用于两位数字。解决此问题的最简单方法是让它采用最后两个字符,并更改选项 <10,以便它们为“Info_01”、“Info_02”、“Info_03”等。
obj.id.substring(obj.id.length-1, obj.id.length);
This code only retrieves the last 1 character of the substring, so it won't work for two digit numbers. The simplest way to fix this is to have it take the last two characters, and change your options <10 so that they're "Info_01", "Info_02", "Info_03", etc.
您有一个子字符串保证返回 1 个且仅返回 1 个字符。总是。
将始终返回“e”。
修复你的子串,你就会好的。
You have a substring guaranteed to return 1, and only 1, character. Always.
will always return "e".
Fix your substring and you'll be good.