繁体   English   中英

如何在 jinja 模板中调用 Javascript function onclick 事件?

[英]How to Call a Javascript function onclick event inside jinja template?

 {% for person in teacher %}
                            <tr class="bg-gray-100 border-b">
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"></td>
                                <td class="p-0 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                    <img src="{{person.customuser.profile_pic.url}}" class="rounded-lg w-32"
                                        alt="Avatar" />
                                </td>
                                <td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap">
                                    <p>{{person.customuser.first_name}} {{person.customuser.last_name}}</p>
                                </td>
                                <td class="text-sm text-gray-900 font-light px-4 py-4 whitespace-nowrap">
                                    {% for item in person.subjects.all %}
                                    <p>{{item.name}}</p>
                                    {% endfor %}
                                </td>
                                <td class="text-sm text-gray-900 font-light px-4 py-4 whitespace-nowrap">

                                    <div class="flex items-center justify-center">
                                        <div id="btnView" class="inline-flex shadow-md hover:shadow-lg focus:shadow-lg"
                                            role="group">
                                            <button type="button"
                                                class="rounded-l inline-block px-6 py-2.5 bg-[#4951bf]  text-white font-medium text-xs leading-tight uppercase hover:bg-blue-700 focus:bg-blue-700 focus:outline-none focus:ring-0 active:bg-blue-800 transition duration-150 ease-in-out">Edit</button>
                                            <button onclick='viewDetails()'
                                                    type="button"
                                                    class=" inline-block px-6 py-2.5 bg-[#4951bf]  text-white font-medium text-xs leading-tight uppercase hover:bg-green-700 focus:bg-green-700 focus:outline-none focus:ring-0 active:bg-green-800 transition duration-150 ease-in-out">View</button>
                                            <button type="button"
                                                class=" rounded-r inline-block px-6 py-2.5 bg-[#4951bf]  text-white font-medium text-xs leading-tight uppercase hover:bg-red-700 focus:bg-red-700 focus:outline-none focus:ring-0 active:bg-blue-800 transition duration-150 ease-in-out">Delete</button>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}

当用户按下按钮时,我想在 onclick 事件中调用 javascript function viewDetails({{person.id}}) 。我希望将参数传递给来自 forid}} 的参数。在 jinja 标签中循环。 可能吗?

您可以set值设置为onclick="viewDetails({{person.id}})"

<button onclick="viewDetails({{person.id}})" type="button" 
class=" inline-block px-6 py-2.5 bg-[#4951bf]  text-white font-medium text-xs leading-tight uppercase hover:bg-green-700 focus:bg-green-700 focus:outline-none focus:ring-0 active:bg-green-800 transition duration-150 ease-in-out">View</button>

然后在你的javascript代码中做任何你想做的事情。

function viewDetails(personId) {
    console.log(personId)
    //do whatever you want to do with that `personId`
}

暂无
暂无

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

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