Javascript SyntaxError:标识符在 Firebug 中的数字文字之后立即开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14966133/
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
SyntaxError: identifier starts immediately after numeric literal in Firebug
提问by Mollo
I'm getting that error when I call this javascript function:
当我调用这个 javascript 函数时,我收到了这个错误:
function kickUser(id_userChat){
$.post("chatFuncs.php", { action: "kick", id_user: id_userChat });
}
this "kickUser" function is generated for every user connected to my chat box, like this
这个“kickUser”函数是为每个连接到我的聊天框的用户生成的,就像这样
$listUsers .= '<img src="imgUsers/'.$DBClass->nomImg($rowUsers['id_user'],$posImg).'" height="'.$heightImg.'" width="'.$widhImg.'"/>
<span class="styleMsg">'.$rowUser['nameUser'].'</span>
<a href="#" class="BtnKick" onClick="kickUser('.$rowUsers['id_user'].')">Kick</a></br>';
and the action "kick" is just an update to my database where I remove the user from my chatUsers table
并且“kick”操作只是对我的数据库的更新,我在其中从 chatUsers 表中删除了用户
If I change $rowUsers['id_user'] for $rowUsers['userName'] the error changes to: ReferenceError: 'userName' is not defined(i changed the real name of the user for 'userName' just for this example).
如果我将 $rowUsers['id_user'] 更改为 $rowUsers['userName'] 错误将更改为: ReferenceError: 'userName' is not defined(我仅在此示例中将用户的真实姓名更改为 'userName')。
回答by the system
Identifiers in JavaScript can't begin with a number; they mustbegin with a letter, $or _.
JavaScript 中的标识符不能以数字开头;它们必须以字母开头,$或者_。
I'm guessing it's coming from this:
我猜它来自于此:
onclick="kick_user('.$rowUsers['id_user'].')">Kick</a>
If you mean to pass a string, then you need to quote the value being passed.
如果您想传递一个字符串,那么您需要引用传递的值。
onclick="kick_user(\"'.$rowUsers['id_user'].'\")">Kick</a>
I don't know PHP, so maybe you need different escaping, but this should give you the idea.
我不知道 PHP,所以也许你需要不同的转义,但这应该给你这个想法。
回答by Bergi
The resulting JavaScript code will be
生成的 JavaScript 代码将是
kickUser(userName)
…and obviously there is no js variable userName. You want to pass a string instead:
...显然没有 js 变量userName。你想传递一个字符串:
kickUser('userName');
So add the quotes/apostrophes to the output, and don't forget to escape the $rowUsers['userName']properly. It's quite the same for $rowUsers['id_user'], which seems to have output even an invalid identifier.
因此,将引号/撇号添加到输出中,并且不要忘记$rowUsers['userName']正确转义。与 for 完全相同$rowUsers['id_user'],它似乎甚至输出了无效的标识符。

