Javascript在href onclick请求中传递变量
我知道这确实是基本的 JavaScript,但由于某种原因,在传递参数时我似乎无法让链接的 onclick 函数正常工作。
我尝试转义引号,添加不同类型的引号并将原始变量添加为字符串。
我让它与下面的内容一起工作,但它说“XYZ 未定义”
function renderLink(value, meta, record)
{
var type = record.data['name']; //value is XYZ
return '<a href="javascript:void(0);" onclick="getReport('+type+'); return false;"></a>';
}
function getReport(type){
alert(type);
}
I know this is really basic javascript but for some reason, I can't seem to get my link's onclick function to work when passing a parameter.
I have tried escaping the quotes, adding different types of quotes and adding the raw variable as a string.
I have it working with the below but it says that "XYZ is undefined"
function renderLink(value, meta, record)
{
var type = record.data['name']; //value is XYZ
return '<a href="javascript:void(0);" onclick="getReport('+type+'); return false;"></a>';
}
function getReport(type){
alert(type);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要转义字符串:
You need to escape the string:
如果您查看呈现的 HTML,您会看到问题:您的
getReport
调用如下所示:我猜您想要围绕它添加引号,所以:
...which renders:
实例
有点深奥,但是当您输出
onclick
属性作为 HTML 源的一部分时,它是当然是 HTML 属性,这意味着您可以使用字符实体。因此,您可以使用"
实体:Live example
我指出这一点并不是因为我推荐它(我不推荐),而是因为记住
onclick
属性中实际发生的情况很有用。这是我强烈建议使用适当的事件处理程序的原因之一(例如,通过addEventListener
/attachEvent
,或者甚至只是分配给a
元素的onclick
属性(一旦实例化)。需要注意的是,这种做法对
record.data['name']
的内容也非常敏感。例如,考虑一下如果Tom's
而不是XYZ
,会发生什么情况。上面第一个选项的输出将是......这显然是一个问题。同样,如果文本中有反斜杠,它将被视为结果中的转义字符,等等——有点雷区。
如果您可以更改您的
renderLink
以便它返回实际实例化的a
元素而不是字符串,这就是我要做的:创建链接和 闭包访问
type
而不将其转换为文本并再次返回。 (如果您不熟悉闭包,请不要担心,闭包是并不复杂。)实例
If you look at the rendered HTML, you'll see the problem: Your
getReport
call looks like this:I'm guessing you want quotes around that, so:
...which renders:
Live example
Somewhat more esoteric, but when you output an
onclick
attribute as part of HTML source, it is of course an HTML attribute, which means you can use character entities. So you could use the"
entity:Live example
I point this out not because I recommend it (I don't), but because it's useful to remember what's really going on in an
onclick
attribute. This is one of the reasons I would strongly recommend using a proper event handler (e.g., viaaddEventListener
/attachEvent
, or even just assigning to thea
element'sonclick
property once it's been instantiated) instead.It's important to note that this way of doing it is also very sensitive to the content of
record.data['name']
. For instance, consider what happens if instead ofXYZ
it'sTom's
. The output of the first option above would be...which is obviously a problem. Similarly, if there's a backslash in the text, it will be treated as an escape character on the result, etc., etc. — a bit of a minefield.
If you can possibly change your
renderLink
so it returns an actual instantiateda
element rather than a string, that's what I'd do:That creates the link and a closure that accesses
type
without turning it into text and back again. (Don't worry if you're unfamiliar with closures, closures are not complicated.)Live example
getReport 接收 XYZ 作为变量而不是字符串,您需要将其放在引号内:
getReport receives XYZ as a variable not as a string, you need to put that inside quotes: