編碼新手。我正在使用 django 框架創建一個庫存表。我希望能夠單擊表行(這是一部分),然后將該行/部分信息解析為詳細視圖。據我了解,表行是內聯元素,內聯元素是偽代碼,當前的 HTML 不允許拋出錨標記(不好的做法?),所以我需要使用一些 javascript。目前,庫存視圖顯示當我使用 runserver ( http://127.0.0.1:8000/inventory/ ) 但單擊任何行時不會執行任何操作。這是我到目前為止所擁有的;庫存.html{% extends "base.html" %}{% block body %}<br><table class="table table-hover"> <thead> <tr> <th>Part Number</th> <th>Description</th> <th>Location</th> <th>Supplier</th> <th>S.O.H</th> </tr> </thead> <tbody> {% for part in parts %} <!-- need to make these table rows link to their respective parts class="table_row" href="{{ selected_part.partnumber }}/detail">{{ selected_part.partnumber }}--> <tr data-href="{% url 'detail' part.pk %}"> <td>{{ part.pk }}</td> <td>{{ part.partnumber }}</td> <td>{{ part.description }}</td> <td>{{ part.location }}</td> <td>{{ part.supplier }}</td> <td>{{ part.stockonhand }}</td> </tr> {% endfor %} </tbody></table>{% endblock %}urls.pyfrom django.urls import pathfrom .views import *urlpatterns = [ path('inventory/', inventory_list, name='inventory'), # URL path for inventory_list view path('<str:pk>/', part_information, name='detail'), path('', index, name='index'),]自定義.js$('tr[data-href]').on("click", function() { document.location = $(this).data('href');});base.html 在標簽<script src="/docs/4.4/dist/js/custom.js"></script>之前</body>。我認為問題出在我的 javascript 文件中。我對此很陌生,非常感謝簡化的解釋
1 回答

料青山看我應如是
TA貢獻1772條經驗 獲得超8個贊
在您的 custom.js 文件中使用以下內容。當頁面加載時,此函數 $(document).ready() 被執行,初始化 tr 的“點擊時”操作
$(document).ready(function(){
$('table tr').click(function(){
window.location = $(this).data('href');
return false;
});
});
- 1 回答
- 0 關注
- 152 瀏覽
添加回答
舉報
0/150
提交
取消