After Alert open modal

Asked

Viewed 81 times

-1

I’m trying to get Alert to open the modal, but it’s not working. In function I check if the field nnfe is filled.

  function TransferenciaPedido() {
if ($("nnfe").val() == undefined) {
    alert("Preencha o número da NF-e corretamente.");
    $('#myModalNFe').modal('show');
}
else {

}}

I’ve tried to put too return false; but even so the modal closeness.

I call the function in a button within the modal:

 <button type="button" class="btn btn-success" onclick="TransferenciaPedido();" data-dismiss="modal">Concluir</button>
  • 1

    Put the code that calls the function to get better

  • 1

    Without the function code it is difficult to help.

  • I put the function. I call it on the button onclick inside the modal.

  • The Alert shoots?

  • @Murilomedeiros yes, it just does not return the open modal.

  • Try . modal() without the show

  • The same thing happens, I’ve tried to create a function just to open, and call her, but it didn’t work either.

  • Take the date-Dismiss="modal" and on the Else Voce put . modal('Hide');

  • 1

    Mariana you are comparing Undefined with nothing, because the val() of $("nnfe") is not returning anything as it is not an HTML tag to be called straight as. So it is missing or a . or a #. You have the browser console open?

  • @Leandrade Look at the value, it is correct the comparison, I just need the modal to be opened after the problem. only this.

  • @Murilomedeiros did it, and continues the same behavior.

  • I think I know where the problem is. Try to get the data-dismiss button, and close the modal on else via JS.

  • What error appears in the browser console? Try to take out the data-dismiss button too

  • @Máttheusspoo I tried this way, but the same problem occurs.

  • @Maikeaerosmith has already done this, and still the modal is not open. No error appears in the browser, the modal just closes.

  • also takes away the $('#myModalNFe').modal('show'); since the modal will remain open and will only close in the else and tries to compare with undefined with ===, is safer.

  • It worked. Thank you.

  • 1

    Arrange. What was happening is that you were initially displaying the modal again before you closed the html button. Then you took the data-Ismiss, but still ordered to open the modal over the modal that was still open.

Show 13 more comments

1 answer

-1

Leaving the answer here for future consultation cases.

The error is in trying to display the modal with it still open. When you call the function the modal has not yet closed, then ends up conflicting. The resolution would be:

function TransferenciaPedido() {
    if ($(".nnfe").val() === undefined) {
        alert("Preencha o número da NF-e corretamente.");
    } else {
        $('#myModalNFe').modal('hide');
    }
}

Html

<button type="button" class="btn btn-success" onclick="TransferenciaPedido();">Concluir</button>

Browser other questions tagged

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