arifpervez arifpervez - 4 months ago 19
jQuery Question

Prevent multiple form submission MVC Jquery

I am using Actionlink and JQuery to submit a form. It is submitting the form every time when clicking the link. I want to submit the form only the first time(once). The code is -

@Html.ActionLink(Model.link, "DefaultRate", "DefaultRate", null, new { @class = "btnclick", onclick = "return false;" })

<script type="text/javascript">
$(document).ready(function () {
$('.btnclick').click(function () {
$(this).closest('form')[0].submit();
});
});
</script>


Thanks in advance

Answer

This must work:

@Html.ActionLink(Model.link, "DefaultRate", "DefaultRate", null, new { @class = "btnclick"})

<script type="text/javascript">
        $(document).ready(function () {
             var allow = true;
             $('.btnclick').click(function () {
                  if (allow){
                      $(this).closest('form')[0].submit();
                      allow = false;
                  }
                  else 
                      return false;    
             });
        });                                              
</script>