delete a record after accepting Bootstrap confirmation message

0

I have a function to delete a record from my BD but before deleting the record I show a confirmation message the problem is that the message is not shown, it goes directly to my function that does the elimination of the BD, what should I do so you can show the confirmation message and if you click on accept now if you go to the function that eliminates

    $('#confirm-delete').on('show.bs.modal', function (e) {
        

            EliminarFormula($(e.relatedTarget).data().id);
   
    });
    
    
    function Eliminar(idElimina) {
     
    var url = "/Validador/Eliminar";
    //var url = "#";
    var data = new FormData();
    data.append("idElimina", idElimina);

    $.ajax({
        type: "Post",
        url: url,
        datatype: "json",
        contentType: false,
        processData: false,
        data: data,
        success: function (data) {
            if (data.Success === true) {

                messageOK('Aviso', data.Mensaje);
            }
            else {

                messageOK('Aviso', data.Mensaje);

            }
        },
        error: function (xhr, status, errorthrow) {

        }

    });
}
<a data-id="123" data-toggle="modal" data-target="#confirm-delete"><span class="glyphicon glyphicon-trash"></span>X</a>
    
asked by Ivxn 22.05.2018 в 01:15
source

1 answer

2

The problem is that you are bindeando the call to the elimination function in the show.bs.modal event that is the event that is thrown when the window is displayed.

I do not know what your HTML is but I understand that it will be something similar to what I put below. In that case you must binde the delete function to the click of the "Ok" button (class .btn-ok ) of the modal window:

$(document).ready(function()
    {
    $('#confirm-delete').on('show.bs.modal',function(){
    		$('.btn-ok').click(function(){
        	console.log("Llamamos a la función de eliminación");
        });
    });
    		
        $('#go').click(function()
        {
            $('#confirm-delete').modal('show');
           
        })
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="modal fade" id="confirm-delete" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Eliminar registro</h4>
                </div>
                <div class="modal-body">
                    <label>¿Estás seguro de eliminar el registro?</label>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default btn-ok" data-dismiss="modal">Sí</button>
                    <a class="btn btn-danger" data-dismiss="modal">No
                    </a>
                </div>
            </div>
        </div>
    </div>
    
    <button id="go">
    X
    </button>
    
answered by 22.05.2018 / 08:32
source