javascript SWITCH语句没有触发

时间:2011-10-27 20:55:57

标签: javascript

我在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肯定有值

我做错了什么?

3 个答案:

答案 0 :(得分:2)

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

检查case parseInt("95") : ...是否有效。

另外,如果您在数字中添加了一个字符,请检查您的案例是否开始有效,然后检查:

switch(checkValue + 'A')

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

答案 1 :(得分:0)

不确定确切的行为,但如果checkValues为null或未定义,我希望它会在该行中断。

答案 2 :(得分:0)

或许checkValue中有空格需要修剪?在default中 - 显示checkValue的价值,这样可以更清晰:

default: 
     alert('no match for "' + checkValue + '"');