Javascript 将单击的表单传递给bootbox.confirm()
我有一个带提交的表单,每个表单都要删除一个对象:Javascript 将单击的表单传递给bootbox.confirm(),javascript,jquery,forms,bootbox,Javascript,Jquery,Forms,Bootbox,我有一个带提交的表单,每个表单都要删除一个对象: {% for person in persons %} <form id='form' name='delete' action="{% url 'delete_person' person.id %}" method='POST'> {% csrf_token %} <button type='submit' onclick="bootbox.confirm();" id="del"
{% for person in persons %}
<form id='form' name='delete' action="{% url 'delete_person' person.id %}"
method='POST'>
{% csrf_token %}
<button type='submit' onclick="bootbox.confirm();" id="del" class='btn btn-xs btn-link icon'><i class='glyphicon glyphicon-remove'></i></button>
</form>
{% endfor %}
我得到的表单url如下所示:
var lHref = $('form#form1').attr('action');
bootbox.confirm("Are you sure?", function(confirmed) {
if(confirmed) {
window.location.href = lHref; ...
问题是,无论我单击哪个删除按钮,表单中的第一个用户都会被删除,因此我想知道如何将
$(此)
传递到bootbox.confirm()
,这样我就可以获得正确的URL,而不是绑定到按钮的id(在您的情况下,该id是多个的,因此它将只绑定第一个按钮的事件)使用表单的提交事件
同时删除onclick按钮
{% for person in persons %}
<form id='form' name='delete' action="{% url 'delete_person' person.id %}"
method='POST'>
{% csrf_token %}
<button type='submit' id="del" class='btn btn-xs btn-link icon'><i class='glyphicon glyphicon-remove'></i></button>
</form>
{% endfor %}
{persons%中的persons%}
{%csrf_令牌%}
{%endfor%}
在javascript中
<script type="text/javascript">
$(document).ready(function(){
$("form").submit(function(e) {
e.preventDefault();
var lHref = $(this).attr('action');
bootbox.confirm("Are you sure?", function(confirmed) {
if(confirmed) {
window.location.href = lHref;
}
});
});
})
</script>
$(文档).ready(函数(){
$(“表格”)。提交(功能(e){
e、 预防默认值();
var lHref=$(this.attr('action');
bootbox.confirm(“确定吗?”),函数(已确认){
如果(已确认){
window.location.href=lHref;
}
});
});
})
使用表单的提交事件,而不是绑定到按钮的id(在您的情况下,该id是多个的,因此它将只绑定第一个按钮的事件)
同时删除onclick按钮
{% for person in persons %}
<form id='form' name='delete' action="{% url 'delete_person' person.id %}"
method='POST'>
{% csrf_token %}
<button type='submit' id="del" class='btn btn-xs btn-link icon'><i class='glyphicon glyphicon-remove'></i></button>
</form>
{% endfor %}
{persons%中的persons%}
{%csrf_令牌%}
{%endfor%}
在javascript中
<script type="text/javascript">
$(document).ready(function(){
$("form").submit(function(e) {
e.preventDefault();
var lHref = $(this).attr('action');
bootbox.confirm("Are you sure?", function(confirmed) {
if(confirmed) {
window.location.href = lHref;
}
});
});
})
</script>
$(文档).ready(函数(){
$(“表格”)。提交(功能(e){
e、 预防默认值();
var lHref=$(this.attr('action');
bootbox.confirm(“确定吗?”),函数(已确认){
如果(已确认){
window.location.href=lHref;
}
});
});
})