【问题标题】:Uncaught SyntaxError: Invalid left-hand side in assignment - Solution?Uncaught SyntaxError: Invalid left-hand side in assignment - 解决方案?
【发布时间】:2020-05-12 08:53:01
【问题描述】:

我在网上查看了有关我遇到的这个问题的其他情况,但没有一个真正为我提供了关于我希望解决的问题的解决方案。我目前设置了一个按钮,因此如果有人按下它,我希望将符号(在这种情况下为加号)打印到输入框中,但目前,我的代码行没有给我解决方案。 对于我的计算器,我设法用我的数字键盘成功地将其拉下来,但是当涉及到符号时,我不知道发生了什么。当我检查控制台时,每当我按下加号按钮时,它都会给我“未捕获的语法错误:分配中无效的左侧”的错误。

<form name = "clear-console">
<input type = "number" id = "input" placeholder = "0">
  <input type = "button" value = "4" onClick = numbers(4) class = "number-pad"></button>
  <input type = "button" name = "add" class = "number-pad" value = "+" onclick = "clear-console.display.value += '+'">   //this line of code
</form>

对我的目标有什么解决方案吗?我在代码中遗漏了什么吗? 感谢您的帮助!

【问题讨论】:

  • clear-` 应该做什么? console.display 是什么?
  • @D.Pardal 对不起,'不应该在清除之后-。它应该是“清晰的控制台”。这是我为我的计算器的“全部清除”功能提供的表格名称。
  • 哦,函数名不能包含-。那是你的问题。也许你的意思是_。

标签: javascript html css


【解决方案1】:
<form name = "clearConsole">  <! –– Remove `-` to make it a valid variable name in JavaScript ––>
<input type = "text" id = "input" placeholder = "0" name="display">  <! –– You are missing `name` for reference of this input ––>
  <input type = "button" value = "4" onClick = numbers(4) class = "number-pad">
  <input type = "button" name = "add" class = "number-pad" value = "+" onclick = "clearConsole.display.value += '+'">   // Now this works.
</form>

您在输入数字时缺少name。此外,您最好避免在表单名称中使用-,因为这在 JavaScript 变量名称中是不允许的。

另外,您需要将该输入的类型更改为text,否则添加后将无法显示1+。

【讨论】:

    【解决方案2】:

    虽然连字符 - 可用于 HTML 属性,但它不能被 JavaScript 引用,因为它是一个运算符。

    将其替换为下划线_(或使用camelCase)就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      相关资源
      最近更新 更多