简体   繁体   English

通过onclick将参数传递给HandlebarsJS partial

[英]Passing parameters via onclick to HandlebarsJS partial

I have a few <div> tags that trigger a modal. 我有一些触发模态的<div>标签。 The modal is a partial included on the page. 模态是部分包含在页面上。 I want to change the text inside the modal depending on the <div> that was clicked. 我想根据单击的<div>更改模式中的文本。 I can't figure out how to pass a variable to the partial when the <div> is clicked. 单击<div>时,我无法弄清楚如何将变量传递给partial。 Any ideas? 有任何想法吗?

<div data-open="config">
    <a href="">link 1</a>
</div>
<div data-open="config">
    <a href="">link 2</a>
</div>
{{> My_modal}}

Please refer to the API documentation of the open event handler. 请参阅open事件处理程序的API文档

You could place here a function that do all the job you need when the dialog is open. 你可以在这里放置一个函数,在对话框打开时完成你需要的所有工作。

But you have to find a way to get your clicked element, as the provided event is just something from jquery dialog. 但是你必须找到一种方法来获取你点击的元素,因为提供的事件只是来自jquery对话框的东西。

You could do by set some css class on selected element on click handler. 您可以通过在单击处理程序上的所选元素上设置一些css类来完成。

And then you get it in other functions when you need. 然后在需要时将其用于其他功能。

But you can do the job in the place where you open the dialog. 但是你可以在打开对话框的地方完成这项工作。

In this function you have access to the clicked element and could get the text easy. 在此功能中,您可以访问单击的元素,并可以轻松获得文本。

And then just compile your handlebars template with your infos. 然后只需使用您的信息编译您的车把模板。

Have a look at this jsfiddle example . 看看这个jsfiddle示例

Just close the dialog and then click on one of the links, you see the text inside the dialog change based on link selected. 只需关闭对话框,然后单击其中一个链接,即可根据所选链接看到对话框内的文本更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM