How to create an Operation Confirmation Alert?

Asked

Viewed 441 times

0

I have the following Function which is called through a OnClick where will delete the record selects. But for security reasons, I would like to put a control to continue the operation or not.

Follows code:

function DeletarLinha(id) {
            var empresa = null;
            var codigo = null;

            var str = id.replace('d', '');
            if (str.indexOf('_') > 0) {
                var res = str.split("_");
                codigo = (res[0]);
                empresa = (res[1]);

                //Alert de Confirmarção
                //Caso for TRUE
                //Executar essa function ConsultarPedido(codigo, empresa);
                //Caso for Falso
                //Apenas abortar o processo.
            }
        }

2 answers

2


You can do it this way:

var button = $("button")

button.on("click", function(){
  var confirmado = confirm('Deseja deletar?');
  if(confirmado){
  	alert('Confirmado!');
  }else{
  	alert('Negado!');
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Confirmar</button>

Using the 'confirm' function you can get it from the user.

Using your own code:

function DeletarLinha(id) {
    var empresa = null;
    var codigo = null;

    var str = id.replace('d', '');
    if (str.indexOf('_') > 0) {
        var res = str.split("_");
        codigo = (res[0]);
        empresa = (res[1]);

        //Alert de Confirmarção
        var confirmado = confirm('Deseja deletar?');
        if(confirmado){
          //Caso for TRUE
          //Executar essa function ConsultarPedido(codigo, empresa);
        }else{
          //Caso for Falso
         //Apenas abortar o processo.
        } 
    }
}

0

You can create a modal and in that modal you would call the function to make the exclusion itself. A quick draft, the code below demonstrates the idea of how it would be

function exclude(){
 document.getElementById('modal').classList.remove('hidden');
 }

function DeletarLinha(id) {
            var empresa = null;
            var codigo = null;

            var str = id.replace('d', '');
            if (str.indexOf('_') > 0) {
                var res = str.split("_");
                codigo = (res[0]);
                empresa = (res[1]);

                //Alert de Confirmarção
                //Caso for TRUE
                //Executar essa function ConsultarPedido(codigo, empresa);
                //Caso for Falso
                //Apenas abortar o processo.
            }
        }
.hidden {
display:none
}
<button onClick="exclude()">Excluir</button>

<div id="modal" class="modal hidden">
<p> Você deseja realmente excluir?</p>
<button onClick="closeModal()">Excluir</button>
<button onClick="DeletarLinha(id)">Excluir</button>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.