我在HTML文件中有一个表格:
{% for dimension in dimension_list %}
<tr>
<td>{{dimension.title}}</td>
<td>{{dimension.description}}</td>
<div class="button-group">
<td><button type="button" class="btn-click-del" data-
id="{{dimension.pk}}" id="btn-confirm" ></button></td>
</div>
</tr>
{% endfor %}
因此,我接收到值data-id(按钮)并将其传递给模型:
$('.btn-click-del').on('click', function () {
var id = ($(this).data('id'));
$("#modal-btn-yes").val(id);
});
在模式中,我需要在“是”按钮中将此id作为参数传递给函数Dimension_remove
div class="modal-dialog modal-bg modal-dialog-centered">
<button onclick="location.href='{% url 'dimension_remove' pk=dimension.id %}';" type="button" class="btn btn-default" id="modal-btn-yes">Yes</button>
<button type="button" class="btn btn-primary" id="modal-btn-no">No</button>
</div>
答案 0 :(得分:1)
我建议将click事件附加到您的JS部分中,并将id
附加到data-id
到modal-btn-yes
上,然后简单地获取它并构建您的URL。
将data-id
附加到modal-btn-yes
按钮上:
$('.btn-click-del').on('click', function() {
$("#modal-btn-yes").data('id', $(this).data('id'));
});
获取data-id
并构建URL:
$('body').on('click', '#modal-btn-yes', function() {
//Here you could get the clicked button "id" like
var id = $(this).data('id');
//location.href= Your link here
});