我这段bootstrap代码生成的Modal对话框,会被执行数次

https://img4.mukewang.com/5c10794d0001039a12500868.jpg

html代码

    <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#dialog" data-action="add">添加</button>

当点击添加按钮后,会先执行alert('1'),然后弹出对话框,我关闭对话框之后,再点击添加,那么这次的alert('1')会被执行两次,也就是说我会收到2次警告框,但是modal对话框会等alert('1')执行完毕了弹出。如果再关掉Modal对话框,再点击按钮,会执行3次alert('1'),以此类推。

那我是不是可以理解为,以下代码会因为点击按钮的次数而被执行多次。

    case 'add':
        modal.find('.modal-dialog').addClass('modal-lg');
        modal.find('.modal-title').text('添加用户');
        modal.find('.modal-body').html('这里是表单');
        modal.find('.modal-footer button[type="submit"]').removeClass('btn-danger').addClass('btn-primary').text ('添加');
        alert('1'); //测试
      break;

那么,我应该如何解决这个问题?


函数式编程
浏览 540回答 1
1回答

饮歌长啸

其实还可以这么解决,,我不是搞前端的,但是这种问题遇见过第一种:把事件放到click事件外去定义$('*[data-target="#dialog"]').click(function(){&nbsp; &nbsp; createDialog();})$(document).on('show.bs.modal','#dialog', function (event) {&nbsp; &nbsp; var button = $(event.relatedTarget);&nbsp; &nbsp; var action = button.data('action');&nbsp; &nbsp; var modal = $(this);&nbsp; &nbsp; console.log(action);&nbsp; &nbsp; switch(action)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case 'add':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modal.find('.modal-dialog').addClass('modal-lg');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modal.find('.modal-title').text('添加用户');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modal.find('.modal-body').html('这里是表单');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modal.find('.modal-footer button[type="submit"]').removeClass('btn-danger').addClass('btn-primary').text ('添加');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('1'); //测试&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }})$(document).on("hidden.bs.modal","#dialog", function() {&nbsp; &nbsp; $(this).removeData("bs.modal");})第二种: 先移除事件再绑定事件$('*[data-target="#dialog"]').click(function(){&nbsp; &nbsp; createDialog();&nbsp; &nbsp; $('#dialog').off('show.bs.modal').on('show.bs.modal', function (event) {&nbsp; &nbsp; &nbsp; &nbsp; var button = $(event.relatedTarget);&nbsp; &nbsp; &nbsp; &nbsp; var action = button.data('action');&nbsp; &nbsp; &nbsp; &nbsp; var modal = $(this);&nbsp; &nbsp; &nbsp; &nbsp; switch(action)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'add':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modal.find('.modal-dialog').addClass('modal-lg');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modal.find('.modal-title').text('添加用户');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modal.find('.modal-body').html('这里是表单');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modal.find('.modal-footer button[type="submit"]').removeClass('btn-danger').addClass('btn-primary').text ('添加');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('1'); //测试&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; //关闭对话框后清除modal中数据&nbsp; &nbsp; $("#dialog").off("hidden.bs.modal").on("hidden.bs.modal", function() {&nbsp; &nbsp; &nbsp; &nbsp; $(this).removeData("bs.modal");&nbsp; &nbsp; });})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript