可以在 Underscore 模板的内联 javascript 中使用对象吗?

发布于 2024-12-27 01:57:35 字数 523 浏览 3 评论 0原文

我正在下划线模板中循环一个数组,以及为每个元素添加 onclick 的内容。我可以以某种方式将对象直接传递给下划线模板中的函数吗?

例如,在这里我试图将客户端元素传递给 editClient() 函数

<table>
    <tbody>
        <% _.each(clients, function(client) { %>
        <tr>
            <td><a href="#" onclick="<% editClient(client); %> return false;"><%= client.name %></a></td>
        <% }); %>
    </tbody>
</table>

我的猜测是下划线只是将所有内容解析为字符串并呈现结果,这意味着我需要将 id 传递给 editClient 函数并获取客户端使用那个。

I am looping an array in an underscore template and what to add onclick for each of the elements. Can I somehow pass the object directly to a function in the underscore template?

e.g. here I'm trying to pass the client element to the editClient() function

<table>
    <tbody>
        <% _.each(clients, function(client) { %>
        <tr>
            <td><a href="#" onclick="<% editClient(client); %> return false;"><%= client.name %></a></td>
        <% }); %>
    </tbody>
</table>

My guess is that underscore just parses everything to a string and renders the result, meaning I would need to instead pass a id to the editClient function and fetch the client using that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

守不住的情 2025-01-03 01:57:35

由于这只是渲染为 html,实际上并没有直接绑定 onclick 事件,因此应该传递 id。

例如

<td><a href="#" onclick="editClient(<%= client.id %>); return false;"><%= client.Name %></a></td>

Since this is just rendering to html and not actually directly binding the onclick event the id should be passed instead.

e.g.

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