语法错误:标识符在数字文字之后立即开始。将 php 变量传递给 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17392619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:10:14  来源:igfitidea点击:

SyntaxError: identifier starts immediately after numeric literal. Passing php variable to JavaScript

phpjavascripthtmlwebliterals

提问by jlagomarsini

I am trying to pass in two variables to a JavaScript function: the input itself and the user id. I call the function using the onclick attribute of the input.

我试图将两个变量传递给 JavaScript 函数:输入本身和用户 ID。我使用输入的 onclick 属性调用该函数。

 echo "<input type = 'submit' class = 'replyButton' id = 'replyButton'
 value = 'reply' onclick = 'insertComment(this, $user_id);'>";

The variables go into a JavaScript function:

变量进入 JavaScript 函数:

function insertComment(input, user)
{
     //use input and user id to carry out tasks.
} 

The error message given is at the function call to 'insertComment()' in the onclick of the input. The error message looks like this:

给出的错误消息是在输入的 onclick 中对“insertComment()”的函数调用。错误消息如下所示:

SyntaxError: identifier starts immediately after numeric literal    

       insertComment(this, 9f60d869342a);

I have tried multiple quote and dot combinations and have checked the other posts on StackOverFlow. They were all to no avail. Your help in resolving this error is greatly appreciated.

我尝试了多个引号和点组合,并检查了 StackOverFlow 上的其他帖子。他们都无济于事。非常感谢您帮助解决此错误。

回答by Patrick Evans

you need to quote the user id

您需要引用用户 ID

echo "<input type = 'submit' class = 'replyButton' id = 'replyButton'
 value = 'reply' onclick = 'insertComment(this, \"$user_id\");'>";