Javascript在href onclick请求中传递变量

发布于 2024-12-11 03:04:33 字数 453 浏览 1 评论 0原文

我知道这确实是基本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羞稚 2024-12-18 03:04:33
return '<a href="javascript:void(0);" onclick="getReport('+type+'); return false;"></a>';

您需要转义字符串:

return '<a href="javascript:void(0);" onclick="getReport(\''+type+'\'); return false;"></a>';
return '<a href="javascript:void(0);" onclick="getReport('+type+'); return false;"></a>';

You need to escape the string:

return '<a href="javascript:void(0);" onclick="getReport(\''+type+'\'); return false;"></a>';
紫轩蝶泪 2024-12-18 03:04:33

如果您查看呈现的 HTML,您会看到问题:您的 getReport 调用如下所示:

getReport(XYZ);

我猜您想要围绕它添加引号,所以:

return '<a href="javascript:void(0);" onclick="getReport(\''+type+'\'); return false;"></a>';

...which renders:

getReport('XYZ');

实例

有点深奥,但是当您输出 onclick 属性作为 HTML 源的一部分时,它是当然是 HTML 属性,这意味着您可以使用字符实体。因此,您可以使用"实体:

return '<a href="javascript:void(0);" onclick="getReport("'+type+'"); return false;"></a>';

Live example

我指出这一点并不是因为我推荐它(我不推荐),而是因为记住 onclick 属性中实际发生的情况很有用。这是我强烈建议使用适当的事件处理程序的原因之一(例如,通过 addEventListener / attachEvent,或者甚至只是分配给 a 元素的 onclick 属性(一旦实例化)。


需要注意的是,这种做法对 record.data['name']内容也非常敏感。例如,考虑一下如果 Tom's 而不是 XYZ,会发生什么情况。上面第一个选项的输出将是

getReport('Tom's');

......这显然是一个问题。同样,如果文本中有反斜杠,它将被视为结果中的转义字符,等等——有点雷区。

如果您可以更改您的 renderLink 以便它返回实际实例化的 a 元素而不是字符串,这就是我要做的:

function createLink(value, meta, record)
{
    var type = record.data['name'];         // Grab value as of when we were called
    var link = document.createElement('a');

    link.href = "javascript:void(0);";
    link.onclick = function() {             // Or even better, addEventListener / attachEvent
        getReport(type);
        return false;
    };

    return link;
}

创建链接和 闭包访问 type 而不将其转换为文本并再次返回。 (如果您不熟悉闭包,请不要担心,闭包是并不复杂。)

实例

If you look at the rendered HTML, you'll see the problem: Your getReport call looks like this:

getReport(XYZ);

I'm guessing you want quotes around that, so:

return '<a href="javascript:void(0);" onclick="getReport(\''+type+'\'); return false;"></a>';

...which renders:

getReport('XYZ');

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:

return '<a href="javascript:void(0);" onclick="getReport("'+type+'"); return false;"></a>';

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., via addEventListener / attachEvent, or even just assigning to the a element's onclick 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 of XYZ it's Tom's. The output of the first option above would be

getReport('Tom's');

...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 instantiated a element rather than a string, that's what I'd do:

function createLink(value, meta, record)
{
    var type = record.data['name'];         // Grab value as of when we were called
    var link = document.createElement('a');

    link.href = "javascript:void(0);";
    link.onclick = function() {             // Or even better, addEventListener / attachEvent
        getReport(type);
        return false;
    };

    return link;
}

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

默嘫て 2024-12-18 03:04:33

getReport 接收 XYZ 作为变量而不是字符串,您需要将其放在引号内:

return '<a href="javascript:void(0);" onclick="getReport(\''+type+'\'); return false;"></a>';

getReport receives XYZ as a variable not as a string, you need to put that inside quotes:

return '<a href="javascript:void(0);" onclick="getReport(\''+type+'\'); return false;"></a>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文