APC
APC

Reputation: 139

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

Upvotes: 1

Views: 139

Answers (2)

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48437

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>

Upvotes: 1

Jawad Umar
Jawad Umar

Reputation: 95

Here is what you want

                <script type="text/javascript">

                    $(document).ready(function () {

                        $('.btnclick').bind("click", function () {

                            $('.btnclick').unbind("click");

                            $(this).closest('form')[0].submit();

                        });
                    });         

                </script>

Upvotes: 0

Related Questions