Javascript:无法使用 onClick 将变量作为参数传递
我有这个代码(jquery):
function somefunction() {
$.ajax({
type : 'POST',
url : 'post.php',
dataType : 'json',
success : function(data){
var mess = "";
var count = 0;
while (count < (data.length - 1))
{
mess = mess + "<a href=# onclick=deletePerson("JohnDoe");return false;><img src=x.gif></a>" + data[count].name + "<br />";
count++;
}
$('#mydiv').html(mess).fadeIn('fast');
},
});
}
function deletePerson(arg) {...}
当我运行这个代码时,一切正常。 但是,当我想通过 onclick 传递变量(而不是“JohnDoe”)时,它会停止工作:
var myvar = "JohnDoe";
mess = mess + "<a href=# onclick=delete(myvar);return false;><img src=x.gif></a>" + data[count].name + "<br />";
我的编辑器告诉我:“未解析的变量或类型”。
传递参数从来没有遇到过问题,但是有了这个 onClick-thing 它就不起作用......
有人知道我做错了什么吗?
多谢!
编辑:约瑟夫的帖子解决了 JohnDoe 问题(谢谢!),但是,当我输入:
var myvar = data[count].name;
它停止工作......有什么想法吗?
I have this code (jquery):
function somefunction() {
$.ajax({
type : 'POST',
url : 'post.php',
dataType : 'json',
success : function(data){
var mess = "";
var count = 0;
while (count < (data.length - 1))
{
mess = mess + "<a href=# onclick=deletePerson("JohnDoe");return false;><img src=x.gif></a>" + data[count].name + "<br />";
count++;
}
$('#mydiv').html(mess).fadeIn('fast');
},
});
}
function deletePerson(arg) {...}
When I run this, everything works fine.
However, when I want to pass a variable (instead of "JohnDoe") with the onclick, it stops working:
var myvar = "JohnDoe";
mess = mess + "<a href=# onclick=delete(myvar);return false;><img src=x.gif></a>" + data[count].name + "<br />";
My editor tells me: "Unresolved variable or type".
Never had a problem with passing parameters, but with this onClick-thing it just doesn't work...
Anyone knows what I'm doing wrong?
Thank's a lot!
EDIT: Joseph's post fixed the JohnDoe problem (thanks!), however, when I put:
var myvar = data[count].name;
it stops working... any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要连接变量
You need to concatenate the variable in
delete
是 javascript 中的保留关键字,您应该重命名您的函数。delete
is a reserved keyword in javascript, you should rename your function.