javascript switch 语句不起作用

发布于 2024-10-21 03:30:03 字数 4824 浏览 4 评论 0原文

您好,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦魇绽荼蘼 2024-10-28 03:30:04

试试这个正则表达式;它将提取对象 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 ];

Try this regular expression; it will extract the entire series of digit characters at the end of the object ID:

var indx = obj.id.match( /([0-9]+)$/ )[ 0 ];

More verbosely, you could toss in a check to make sure there was a match:

var m = obj.id.match( /([0-9]+)$/ );

if ( m.length == 0 )
    return;

var indx = m[ 0 ];
始于初秋 2024-10-28 03:30:04

我知道这与上面所说的初始问题不同,该问题使用字符串类型变体作为 switch 语句的参数,但我想在这里发布一个“陷阱”,因为它与“javascript”相同switch 语句不起作用”。

我想要介绍的场景是当您尝试使用数字类型变体作为 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 也好不到哪里去,我见过它做了一些非常非常令人难以置信的坏事,例如评估一个显然是 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:

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;
}

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.

intCase = new Number(intCase) + 0; //This is like type casting

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.

丢了幸福的猪 2024-10-28 03:30:04

该问题是由于您使用了 substring() 造成的。

The issue is due to your use of substring().

王权女流氓 2024-10-28 03:30:04

使用:

var indx = obj.id.match( /\d+$/ )[0];

查找您的索引。
正则表达式匹配字符串末尾的一个或多个数字。

use:

var indx = obj.id.match( /\d+$/ )[0];

to find your index.
The regular expression matches one or more digits at the end of a string.

眉目亦如画i 2024-10-28 03:30:03

您的子字符串始终只有一个字符长:

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;
        }
    }
}

Your substring is always just one character long:

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

By the way: If you want to disable all other checkboxes than the checked one, you can do this:

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;
        }
    }
}
善良天后 2024-10-28 03:30:03

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.

筱果果 2024-10-28 03:30:03

您有一个子字符串保证返回 1 个且仅返回 1 个字符。总是。

var s = "sample";
s.substring(s.length-1,s.length);

将始终返回“e”。

修复你的子串,你就会好的。

You have a substring guaranteed to return 1, and only 1, character. Always.

var s = "sample";
s.substring(s.length-1,s.length);

will always return "e".

Fix your substring and you'll be good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文