亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

bootstrap 中的 modal 模糊框如何獲取點擊按鈕事件?

bootstrap 中的 modal 模糊框如何獲取點擊按鈕事件?

函數式編程 2019-03-04 13:14:17
我自己封裝了一個js函數!使用起來有些問題,我設定了callback,會隨著點擊次數的增加,執行次數也會增加,不知道是什么原因!js封裝的函數showConfirm(title , content , showCancel , success , cancel)    {        showCancel = showCancel === false ? showCancel : true;        $('#showModal .modal-title').text(title);        $('#showModal .modal-body').text(content);        if(!showCancel)        {            $('#showModal .cancel').hide();        }        $('#showModal .cancel').eq(0).click(cancel);//取消按鈕DOM        $('#showModal .success').eq(0).click(success);//確定按鈕DOM        $('#showModal').modal();    }前端調用<button class="btn btn-info btn-sm" onclick="test()">test</button><script>        function test()        {            pages.showConfirm('測試','測試內容',true,function(){                console.log(1);            },function(){                console.log(2);            })        }    </script>console.log中打印的次數會隨著點擊次數不斷遞增!
查看完整描述

1 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

你的<button>里用的是onclick="test()",也就是每點擊一次,都會去執行test()函數,然后每次test()執行的內容showConfirm當中都會去注冊一次.cancel和.success的click事件——輸出1|2,也就是每點擊一次,就多注冊一次,然后多輸出一次1|2。你可以去掉button當中的onclick="test()",然后這么寫:


<button class="btn btn-info btn-sm" id="test_button">test</button>


<script>

    $('#test_button').click(showConfirm('測試','測試內容',true,function(){

        console.log(1);

    },function(){

        console.log(2);

    }));

</script>

這樣你的button只注冊一次點擊事件即可。

希望能幫助到你。


查看完整回答
反對 回復 2019-03-27
  • 1 回答
  • 0 關注
  • 397 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號