解决未捕获的SyntaxError:意外的标识符

时间:2015-10-09 22:47:06

标签: javascript

我正在尝试创建一个简单的脚本,从textarea元素获取文本,然后将该文本发送到警告框。 我在检查chrome web工具上的代码时收到错误Uncaught SyntaxError:意外的标识符,并希望了解为什么会发生这种情况,谢谢。

此行if input == "" {

上发生错误

这是我的代码

    var submit = document.getElementById('submitbutton');
    submit.click(function(){
        var input = document.getElementById('inputbox').value;
        if input == "" {
            console.log('there was no input');
        } else {
            console.log(input);
        }

3 个答案:

答案 0 :(得分:1)

添加括号

if(input == "")

答案 1 :(得分:0)

你的语法很糟糕(不工作)。第二件事是你尝试在DOM元素中使用jQuery库什么都行不通。 这是更正后的代码。

var send = function () {
  var input == docment.getElementById('inputbox').value;
  if (input === '') {
    console.log('there was no input');
  } else {
    console.log(input);
  }
};
document.getElementById('submitbutton').addEventListener('click', send);

尝试为编辑器安装JShint以避免语法错误并继续学习。 :-)如果你想使用submit.click();你需要将jQuery添加到你的项目中并使用jQuery来“抓取元素”

答案 2 :(得分:0)

var submit = document.getElementById('submitbutton');
submit.click(function(){
var input = document.getElementById('inputbox').value;
if (input == "") {
    console.log('there was no input');
} else {
    console.log(input);
}

如果/ else语句要求条件在一对括号中(第4行)