开发者

javascript switch statement not working

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?


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


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.


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.


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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜