最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Callback when we use data-target in bootstrap - Stack Overflow

programmeradmin5浏览0评论

I am working with a single page application, There are some buttons which which opens this modal through data-target.

I need to hit an API when this "MODAL" opens up on screen through any of the button clicks. (There are multiple buttons which could open this modal)

One very messy way would be to attach an onclick event to every button. Could anyone suggest me a cleaner way to acplish this in which I could probably attach some kind of event handler to the modal?

here is the messy solution of my problem

<a data-target="#SomeModal" data-toggle="modal" href="#">
<button class="btn btn-success btn-lg" onclick="function_api_call();">
Click me</button>
</a>

I am working with a single page application, There are some buttons which which opens this modal through data-target.

I need to hit an API when this "MODAL" opens up on screen through any of the button clicks. (There are multiple buttons which could open this modal)

One very messy way would be to attach an onclick event to every button. Could anyone suggest me a cleaner way to acplish this in which I could probably attach some kind of event handler to the modal?

here is the messy solution of my problem

<a data-target="#SomeModal" data-toggle="modal" href="#">
<button class="btn btn-success btn-lg" onclick="function_api_call();">
Click me</button>
</a>
Share Improve this question asked Jun 1, 2015 at 14:08 Namit SingalNamit Singal 1,51612 silver badges26 bronze badges 1
  • jQuery: $('.btn.btn-success').on('click', function_api_call); – lshettyl Commented Jun 1, 2015 at 14:13
Add a ment  | 

2 Answers 2

Reset to default 13

You can listen to the modal show event :

$('#SomeModal').on('show.bs.modal', function (e) {
    // do something...
})

It will be called each time the modal is called, from direct js call and from data attribute

For reference (Bootstrap modal event list) : http://getbootstrap./javascript/#modals-events

add a class for the button for-example: myModalButtonsClass, and add an onclick event for that class instead of adding an onclick event for each and every button

$(".myModalButtonsClass").click(function(){ 


});
发布评论

评论列表(0)

  1. 暂无评论