尝试捕获简单的输出错误

时间:2016-01-14 01:39:16

标签: javascript html

我是新来的,我真的不知道这个代码的问题..请帮助.. Tnx!这是我的整个.html文件 PS:即使我包含表单标签,也没有任何反应

<input type="text" id="bitch" name="try1" required />
<button onClick="trycatch()">Click</button>

这是我的脚本标签..

function trycatch() {
    var try1 = document.getElementById('bitch').value;
    try {
        if(try1 == "") {
            throw"empty1";
        }
        else if (isNaN(try1)) {
            throw"empty2";
        }
        else if((try1 > 100) || (try1 < 0)) {
            throw"invalid";
        }
    }
    catch(error) {
        if(error == "empty1") {
            alert("Please enter a number!");
        }
        if(error == "empty2") {
            alert("Make sure it is a number!");
        }
        if(error == "invalid") {
            alert("Number range is 1-100!");
        }
    }

    if(try1 <= 99) {
        document.write("Your score is " try1);
    }
    else if(try1 == 100) {
        document.write("You got a perfect score!");
    }
}

2 个答案:

答案 0 :(得分:1)

您错过了+符号:

document.write("Your score is " + try1);

答案 1 :(得分:0)

您的javascript语法中存在错误,此行失败:

document.write("Your score is " try1);

将其更改为:

document.write("Your score is " + try1);