Get button data in jQuery and call Modal

1

I have the following button:

<button type="button" class="btn btn-danger btn-custom" name="deletar" id="<? echo $valor->set_cod; ?>"><i class="fa fa-trash"></i></button>

I need to get the id="", and after searching for the id, I would like to call the bootstrap modal myself, with the delete message, which in turn does the action when the OK button is pressed. How do I?

    
asked by anonymous 08.08.2016 / 19:50

2 answers

1

You can use the data-href attributes with the url to delete the record. Ex: data-href="delete.php? Id = set_cod;? >"

With jquery the following code, guarantees the action when confirming

$('#nome-sua-modal').on('show.bs.modal', function(e) {
  //btn-ok é a classe do botão confirmar na modal
  $(this).find('.btn-ok').attr('href',        $(e.relatedTarget).data('href'));
});

Here is a complete example: example

    
08.08.2016 / 20:35
1

To open a modal in bootstrap you use data-target :

<button type="button" class="btn btn-primary btn-lg" 
    data-toggle="modal" data-target="#myModal">

 <div class="modal fade" id="myModal" 
    tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
 </div>

If you need to do something in the click event of the button you can use onclick=minhaFuncao(id) passing the id to the function.

function minhaFuncao(id) {
    // fazer algo aqui
}

If the modal id is obtained via php, you can use the same way of passing the id for both cases (button's data-id and modal id).

    
08.08.2016 / 20:39