Calling function after opening Modal

Asked

Viewed 1,816 times

1

I have this Jquery code:

<script>
jQuery(document).ready(function(){
  // Numero de click
var totalClicks = 0;

jQuery('#addpayment').submit(function(){
    var dados = jQuery( this ).serialize();

     // Verifica se o valor da variável totalClicks representa 3 clicks.
    // Caso o número seja menor que 3 clicks, ignora esse trecho
    if (totalClicks >= 2) {
       alert("redirecionando");
       windows.location......
            return false;   
    }

}


           //Soma o valor de totalClicks + 1
  totalClicks++;

});

</script>

And this modal:

<form action="" method="post" id="addpayment" name="addpayment" class="form-
 horizontal">

<!-- Modal -->

<div class="container">

  <!-- Trigger the modal with a button -->



  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h6 class="modal-title">Teste - Modal </h6>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

</div>

<!--Fim Modal -->

It needed that after each click appeared the modal above, and in the third click, redirected the user to a page. Modal works, but jquery events (click) do not.

  • is correctly incrementing the variable totalClicks?

  • Theoretically yes. It takes the click by jQuery('#addpayment'). Submit(Function() and feeds in totalClicks++;. I may be mistaken, but I believe you are right. Unless the problem is in the Modal call breaking this function.

  • tries to put a console.log(totalClicks) or a alert(totalClicks), may be he’s resetting his counter

  • I put the alert (Alert(totalClicks)) inside jQuery('#addpayment'). Submit(Function(){, however only opens the modal, it seems that neither enters the function.

  • does not increment, no... is out of Submit’s Function it adds in Document ready and only

  • Not if I know is a mistake when copying the code, but in the example the line totalClicks++; is not within the anonymous function.

  • even though @Leandroangelo commented that he was suspicious.

Show 2 more comments

2 answers

0

According to the example, the variable increment totalClicks is outside the scope of the anonymous function, just when the event occurs submit the value is not incremented.

The right thing would be:

<script>
  // Numero de click
  var totalClicks = 0;

  jQuery(document).ready(function(){
      jQuery('#addpayment').submit(function(){
          e.preventDefault();
          var dados = jQuery( this ).serialize();

          // Verifica se o valor da variável totalClicks representa 3 clicks.
          // Caso o número seja menor que 3 clicks, ignora esse trecho
          if (totalClicks >= 2) {
              alert("redirecionando");
              windows.location......
              return false;   
          }

          //Soma o valor de totalClicks + 1
          totalClicks++;
      }
  });
</script>
  • I inserted your suggestion, but even so it only calls the modal (popup), if I click 10 times, it opens 10 times the modal, not entering the function.

0


First thing to note is: if you submit the form, the variable totalClicks will be lost because it will either reload the current page or go to another page.

If you are going to use Ajax on Submit, have to put the method e.preventDefault(); to prevent page reloading.

Below, I reorganized the code to work the redirect after the third click on the button Send.

What has been done?

When the variable value totalClicks is equal to 1, the attribute value has been removed data-toggle from the button, preventing the modal to open again, and at the next click the redirect will occur.

jQuery(document).ready(function(){

// Numero de click
   var totalClicks = 0;

   jQuery('#addpayment').submit(function(e){
      e.preventDefault();

      var dados = jQuery( this ).serialize();

      // Verifica se o valor da variável totalClicks representa 3 clicks.
      // Caso o número seja menor que 3 clicks, ignora esse trecho
      if (totalClicks >= 2) {
        alert("redirecionando");
        window.location = "alguma_página";
         return false;   
      }else if(totalClicks == 1){
         $("button[data-toggle='modal']").attr("data-toggle","");
      }

      //Soma o valor de totalClicks + 1
      totalClicks++;
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<form action="" method="post" id="addpayment" name="addpayment" class="form- horizontal">

   <!-- Modal -->

   <div class="container">

      <!-- Trigger the modal with a button -->

      <button class="btn btn-primary" data-toggle="modal" data-target="#myModal">
         Enviar
      </button>

      <!-- Modal -->
      <div class="modal fade" id="myModal" role="dialog">
         <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
               <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal">&times;</button>
                  <h6 class="modal-title">Teste - Modal </h6>
               </div>
               <div class="modal-body">
                  Corpo da modal
               </div>
               <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
               </div>
            </div>
         </div>
      </div>
   </div>
</form>

  • Dvd, so maybe my form is updating and not increasing my variable? Because your modal worked cool, but my form continues only opening Modal without redir.

  • Yes. As I said, when submitting the form, the page was updating itself, going back to the beginning. Explain better what you want it to do.

  • I have my form and in it a submit button. By clicking this submit button, it will call the modal with a specific text. After calling this modal 2 times, in the third redirect to a page. You did redirect through the modal window, I need it to be by my form.

  • @user54154 where is the code that opens the modal? It is probably before the if (totalClicks >= 2) {, soon will always open the modal, no matter the value of totalClicks .

  • @Filipe, the modal is tied with the "send" button through (data-toggle="modal" data-target="#myModal"). It has an id in the modal called #myModal. I believe it is the problem that the DVD said, because it used the same code that I have and worked, but the button that makes the redir is what this modal, not updating when you click on "send".

  • @user54154 Updated response as you indicated.

  • [SOLVED} thank you.

Show 2 more comments

Browser other questions tagged

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