Javascript:简单的功能,var没有设置

时间:2011-08-01 02:14:55

标签: javascript

我在这里做错了什么?我甚至尝试使用case代替if,但它不起作用

function updateResultPage(the_resp,r) {
    var to_alert="s";

    if(the_resp=="11"){
        to_alert="Thank you!!";
    } 
    else if(the_resp=="22"){
        to_alert="Error:.";
    }
    else if(the_resp=="33"){
        to_alert="ERROR 234dfR,.";
    }

    alert("c "+to_alert+the_resp);
}

我收到显示C s22

的提醒

为什么它会跳过所有if()语句?

编辑: 好的,我添加了此代码以查看resp的确切值:alert("d "+to_alert+" *"+the_resp+"*"); 而第二个*正在下一行,所以看起来我有一个修剪问题......

3 个答案:

答案 0 :(得分:3)

添加

the_resp = the_resp.replace(/^\s*|\s*$/g,"");  // will trim it.

在你的功能开始时。

答案 1 :(得分:2)

删除此

alert("c "+to_alert+the_resp);

或添加else

else{
    alert("c "+to_alert+the_resp);
}

答案 2 :(得分:1)

可能值得检查以确保传入实际的字符串或字符串对象(而不是说int)。也值得确保通过适当修剪来确保输入中没有任何非打印空白。