简体   繁体   中英

jQuery : button_click event not fires on first click and works on second click, why? How to fix it?

This is my code and the below submit button is not properly triggered during first click. During second click, the page get redirected to the next page using window.open() method. What could be the issue cause this?

<div>
    <button type="button" class="submit btn btn-default" id="btnSubmit">Submit 
    </button>
    <button type="button">Cancel</button>
</div>




<script>
$("#btnSubmit").click(function(e)
 {
  e.preventDefault();

  //btn Click validation and code submission goes here..

   alert("Form has been successfully submitted");
   window.open("http://....../viewmyrequest.aspx","_self"); 

  });

</script>

Might be due to missing closing parenthesis.

<div>
    <button type="button" class="submit btn btn-default" id="btnSubmit">Submit 
    </button>
    <button type="button">Cancel</button>
</div>

<script>
$("#btnSubmit").click(function(e) {
  e.preventDefault();

  //btn Click validation and code submission goes here..
   alert("Form has been successfully submitted");
   window.open("http://....../viewmyrequest.aspx","_self"); 
});
</script>

Hi please include your click event code in

$ (document).ready(function(){ });

Like below

$(document).ready(function(){
    $("#btnSubmit").click(function(e)
    {
    e.preventDefault();

    //btn Click validation and code submission goes here..
    alert("Form has been successfully submitted");
    window.open("http://....../viewmyrequest.aspx","_self"); 
  });
});

Please find the demo here https://jsfiddle.net/Prakash_Thete/k9uym8cf/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM