Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将单击的表单传递给bootbox.confirm()_Javascript_Jquery_Forms_Bootbox - Fatal编程技术网

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;
}
});
});
})