JavaScript SWITCH语句未触发

3

我在ASP.NET C# web应用程序中有一个JavaScript的switch语句。我正在使用FireBug调试它,但是它出现错误:一旦到达switch语句,就会立即退出。

以下是代码:

$(function() {

    $('#received_dateTextbox').mask("99/99/99");
    $('#report_dateTextBox').mask("99/99/99");
    $('#occurrence_dateTextBox').mask("99/99/99");

    //var checkValues='';

    $('table input:checkbox').click(function() {

        if ($(this).prop('checked')) {
            var checkText = $(this).next('a').text();
            var hrefValue = $(this).next('a').attr('href');
            var trimIndex = hrefValue.lastIndexOf('\\') + 1;
            var checkValue =  hrefValue.substr(trimIndex, hrefValue.indexOf("')",trimIndex)-trimIndex);

            //checkValues+=checkValue+";";

            switch(checkValue)
            {
                //preanalytical other
                case "21": 
                    var userInput = prompt("Other:", "Other:");
                    $(this).next('a').html('Other:' + '<font color="red">' + userInput + '</font>');
                    checkValues+=checkValue+":"+userInput;
                break;

                //analytical other
                case "53": 
                    var userInput = prompt("Other:", "Other:");
                    $(this).next('a').html('Other:' + '<font color="red">' + userInput + '</font>');
                    checkValues+=checkValue+":"+userInput;
                break;

                //postanalytical other
                case "89": 
                    var userInput = prompt("Other:", "Other:");
                    $(this).next('a').html('Other:' + '<font color="red">' + userInput + '</font>');
                    checkValues+=checkValue+":"+userInput;
                break;

                //other other
                case "95": 
                    var userInput = prompt("Other:", "Other:");
                    $(this).next('a').html('Other:' + '<font color="red">' + userInput + '</font>');
                    checkValues+=checkValue+":"+userInput;
                break;

                //analytical liquid handler instrument failure
                case "40":
                    var userInput = prompt("Liquid Handler#:", "Liquid Handler#:");
                    $(this).next('a').html('Liquid Handler#:' + '<font color="red">' + userInput + '</font>');
                    checkValues+=checkValue+":"+userInput;
                break;

                //analytical olympus instrument failure
                case "41":                   
                    var userInput = prompt("Olympus#:", "Olympus#:");
                    $(this).next('a').html('Olympus#:' + '<font color="red">' + userInput + '</font>');
                    checkValues+=checkValue+":"+userInput;
                break;

                //analytical lcms instrument failure
                case "42":
                    var userInput = prompt("LC-MS/MS#:", "LC-MS/MS#:");
                    $(this).next('a').html('LC-MS/MS#:' + '<font color="red">' + userInput + '</font>');
                    checkValues+=checkValue+":"+userInput;
                break;

                //analytical liquid handler delay prod
                case "49":
                    var userInput = prompt("Liquid Handler#:", "Liquid Handler#:");
                    $(this).next('a').html('Liquid Handler#:' + '<font color="red">' + userInput + '</font>');
                    checkValues+=checkValue+":"+userInput;
                break;

                //analytical olympus delay prod
                case "50":
                     var userInput = prompt("Olympus#:", "Olympus#:");
                    $(this).next('a').html('Olympus#:' + '<font color="red">' + userInput + '</font>');
                    checkValues+=checkValue+":"+userInput;
                break;

                //analytical lcms delay prod
                case "51":
                    var userInput = prompt("LC-MS/MS#:", "LC-MS/MS#:");
                    $(this).next('a').html('LC-MS/MS#:' + '<font color="red">' + userInput + '</font>');
                    checkValues+=checkValue+":"+userInput;
                break;

                //wrong practice code
                case "63":
                    if (confirm("Do you want to check Report Sent to Wrong Location/Physician?"))
                    { var elNode = document.getElementById("TreeView1n82CheckBox");
                        $(elNode).prop("checked", true);
                     }
                    else
                    { var elNode = document.getElementById("TreeView1n81CheckBox");
                        $(elNode).prop("checked", false);
                    }

                break;  

                default: 
                    alert('no match');                
            }
        }
    });
    //document.getElementById('HiddenField1').value = checkValues;
});

调试器向我展示了 checkValue 一定有值。
我做错了什么?

1
没有错误?你确实在你的实际代码中结束了 switch 语句吗? - pimvdb
但它是否具有您已经为之进行了“case”的值?如果您在switch中添加“default:alert('no match');”,那么警报会显示吗? - Marc B
@marc 是的,它确实有我寻找的值。 - Alex Gordon
@pimvdb 是的,我发布了我的代码的其余部分。 - Alex Gordon
如果弹出警报,则表示您的“case”值都不匹配。 - Marc B
显示剩余4条评论
3个回答

2

checkValue可以存储为数字,而不是字符串,因为所有的“case”都是检查数字。

检查case parseInt("95") : ...是否起作用。

此外,如果您在数字后添加一个字符,然后检查该字符是否起作用,也请检查您的情况:

switch(checkValue + 'A')

然后case "53A": .... break;


你在switch()之前尝试过alert(checkValue);吗?这些值都正确吗? - Leon
2
我不理解 - checkValue 明显是一个字符串。 - Tom
@Tom:checkvalue不是字符串,实际上是一个数字(!)。请参考测试代码:http://jsfiddle.net/3MXtM/ - Leon
@lttlrck:substr 可能会返回字符串,但由于该字符串看起来像一个数字(即所有数字),JS 引擎可能会做一些奇怪的事情。JS 不是强类型的。 - Leon
1
@Leon 你的示例代码有误;如果可能,isNaN()会将字符串强制转换为数字,它并不能证明原始类型。正确的测试在这里:http://jsfiddle.net/Trmjt/1/,它显示它是一个字符串。 - user172783
显示剩余2条评论

0

不确定确切的行为,但如果checkValues为空或未定义,我会期望它在那一行中断。


它在上面被定义为 var checkValues = ''; - Alex Gordon
我已经完全删除了它,但结果仍然相同。 - Alex Gordon

0
也许你需要将 checkValue 中的空格去掉?在你的default中,将checkValue的值显示出来,这样应该更清楚:
default: 
     alert('no match for "' + checkValue + '"');

我已经检查过了,确实是"95"或"63"等。 - Alex Gordon
我刚刚尝试了一下,对于没有匹配的情况它是有效的,但是如果有匹配的话,它就不会进入"case"语句。 - Alex Gordon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接