由于按钮onclick,Bootstrap确认不起作用

时间:2017-06-09 11:21:12

标签: jquery twitter-bootstrap confirmation

<script src="path/to/jquery.js"></script>
<script src="path/to/bootstrap.js"></script>
<script src="path/to/bootstrap-confirmation.js"></script>

我使用Bootstrap确认。

这不起作用:

<a class="btn btn-danger btn-xs" data-toggle="confirmation" 
   onclick="DeleteUser(this);" data-id="10"><span class="fa fa-plus">
 </span> Delete</a>

这与同一页面有效:

  <button class="btn btn-default" data-
 toggle="confirmation">Confirmation</button>
  <a class="btn btn-default" data-toggle="confirmation">Confirmation1</a>

我在文档准备就绪时的确认设置

 $('[data-toggle="popover"]').popover({ placement: 'left', html: true, trigger: 'hover' });
    $('[data-toggle=confirmation]').confirmation({ btnOkLabel: 'Yes', btnCancelLabel: 'No', title: 'Are you sure?' });

更新我的ajax删除功能:

function DeleteUser(ele) {
    var id = $(ele).attr('data-id');
    $.ajax({
        type: 'POST',
        cache: false,
        url: '../ashx/FriendOperation.ashx',
        data: { id: id, rol: ' ' ,op:<%=(int)CrudOp.Delete%>},
        beforeSend: function () {
           //bla bla
        },
        success: function (data) {
            var jsonData = JSON.parse(data);
            if (jsonData != null) {
                if (parseInt(jsonData) > 0) {

                    alert('okey');
                    GetFriendOp();
                } else {
                  alert('expception');
                }
            } else {

                alert('expception');
            }
        }
    });

}

我可以像$('#element').confirmation('show');

这样与AJAX结合使用

成功

  

$(&#34; [data-toggle =确认]&#34;)。确认({btnOkLabel:&#39;是&#39;,btnCancelLabel:&#39; No&#39;,标题:& #39;你确定吗?&#39;,容器:&#34; body&#34;,btnOkClass:&#34; btn btn-sm btn-success btn-xs&#34;,btnCancelClass:&#34; btn btn-sm btn-danger btn-xs&#34;,onConfirm:function(event,element){alert(&#39;确认点击&#39;);}});

谢谢@DanCouper。

1 个答案:

答案 0 :(得分:1)

内联onclick处理程序将取消绑定任何其他事件处理程序。您基本上覆盖了引导程序确认所做的,在这种情况下甚至使用确认是完全没有意义的,因为您只是编写自己的逻辑。查看Boostrap Confirm文档,它为您提供了添加逻辑的钩子。不要只将onclick属性添加到已定义的事件(事件监听器)行为和允许您执行您现在尝试执行的操作的API(理想情况下,根本不使用onclick属性)如果可能,请使用HTML,但那是其他内容)

相关问题