简体   繁体   中英

Boostrap Modal doesn't work

Hello I hope you can help me with the problem I'm having right now.

I'm trying to execute a code in JS using modals when pressing a button, the thing is, the HTML code who has the button generates itself

Main View page

<tr class="odd">
<td class=" sorting_1">CU</td>
<td class="">Cliente Unico</td><td class="">101</td>
<td class="">
<span id="edit_CU" class="ui-icon ui-icon-pencil" data-toggle="modal" data-target="#modal_edit"></span>
<span id="delete_CU" class="ui-icon ui-icon-trash" data-toggle="modal" data-target="#modal_delete"></span>
</td>
</tr>

In the id="edit_CU" the CU changes when the HTML code is generated, so is not always the same thats also with the id="delete_CU"

When you click in the span icon will trigger the modal and show this:

View page when you click Edit or Delete button

<div id="modal_delete" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Delete vertical</h4>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to delete?</p>                 
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal_delete">Yes</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="<?php echo base_url('assets/js/helper/func_verticales.js');?>"></script>

In this view page ask the user if they want to delete their information when they click "Yes" it should pop up a message... but it does nothing, someone can give me a hand?

This is the JavaScript

 $("#modal_delete").on('show.bs.modal', function(event){
    alert("hello"); 
  });
});

Run that alert off your button, not the modal

 <button id="modal_delete">Yes</button>

 $("#modal_delete").on('click', function(event){
     alert("hello"); 
   });
 });

or use a confirm box

$('#modal_delete').click(function(ev){
        ev.preventDefault();
         var r = confirm("Are you sure?");
         if (r == true) {
            //run delete code here
         }
    });

if i understand you correctly than you've to delegate the click event try something like this:

Your button needs some adaption - because i don't see a reason for data-toggle and data-target

<button type="button" class="btn btn-primary">Yes</button>

After that - in your JS Code:

$("button.btn-primary").on("click","#modal_delete",function()   
{
    alert("hello");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM