Unexpected SyntaxError: Unexpected end of input

Unexpected SyntaxError: Unexpected end of input

“Uncaught SyntaxError: Unexpected end of input”是我一直收到的关于表单下方按钮的错误消息。我在下面的代码中用 * * 标记了下划线(为了进一步参考,它在我下面的示例代码的最后,只有一个带下划线的“>”)。我快要变白了,求助!

<form id="myForm2">

   <label for="milesToDrive">Miles to drive:
   </label><br>
   <input type="number" id="milesToDrive" name="milesToDrive" value="100"><br>

   <label for="xRatio">X Ratio:
   </label><br>
   <input type="number" id="xRatio" name="xRatio" value="3"><br>

   <label for="yRatio">Y Ratio:
   </label><br>
   <input type="number" id="yRatio" name="yRatio" value="1"><br><br>

</form>

<button type="submit" form="myForm1" value="Submit" onclick="return myCar.drive( document.getElementById("milesToDrive").value , document.getElementById("xRatio").value , document.getElementById("yRatio").value )">Drive the car</button*>*

有问题的部分在这里:

onclick="return myCar.drive( document.getElementById("milesToDrive").value , document.getElementById("xRatio").value , document.getElementById("yRatio").value )"

开始 onclick 函数的引号由开始 milesToDrive 的引号结束。要解决此问题,您可以使用单引号来分隔 onclick 函数。

onclick='return myCar.drive( document.getElementById("milesToDrive").value , document.getElementById("xRatio").value , document.getElementById("yRatio").value )'

或者,如果您需要在函数中同时使用单引号和双引号,您可以使用 &quot; 来转义双引号。

onclick="return myCar.drive( document.getElementById(&quot;milesToDrive&quot;).value , document.getElementById(&quot;xRatio&quot;).value , document.getElementById(&quot;yRatio&quot;).value )"