[英]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.