当没有任何错误时,Javascript给了我一个错误

Javascript is giving me an error when there isn't any

本文关键字:错误 一个 Javascript 任何      更新时间:2023-09-26

这段代码工作正常,现在我总是得到一个"意外的令牌"

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        if (xmlhttp.responseText.search("url=") == 0){
            $("#showMessage").after('<div class="message"><center>Copy your new URL:<input type="text" disabled="disabled" value="'+xmlhttp.responseText.substr(4)+'"</input><br /></center></div>');
        else{   
            $("#showMessage").after('<div class="error">'+xmlhttp.responseText+'</div>');
        }
    }
}

这就是所谓的:

<input type="submit" id="btn-submit" value="Submit" onclick="javascript:createURL()"/>

(我也得到一个"未定义createURL",但我猜这是因为语法错误)

您需要

else之前使用右括号。

if (xmlhttp.responseText.search("url=") == 0){
   $("#showMessage").after('<div class="message"><center>Copy your new URL:<input type="text" disabled="disabled" value="'+xmlhttp.responseText.substr(4)+'"</input><br /></center></div>');
} else{   
    $("#showMessage").after('<div class="error">'+xmlhttp.responseText+'</div>');
}

if ... else语句中存在语法错误

现在是:

if(){
...
//<< There should be a closing bracket for the if statement here
else{
...
}

但它应该是:

if(){
...
}
else{
...
}

或者,如果 ifelse 子句中的代码位于一行上,则可以跳过括号:

if() ...;
else ...;
相关文章: