1

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

5
  • @Satpal, I am new in JQuery, how to do this, please can you give me an example Commented Jul 19, 2016 at 12:02
  • 1
    jQuery.one() Commented Jul 19, 2016 at 12:03
  • ^^^^ is better solution Commented Jul 19, 2016 at 12:04
  • 1
    And do not miss the e in one, Commented Jul 19, 2016 at 12:07
  • Hi, I tried but did not work, <script type="text/javascript"> $(document).ready(function () { $('.btnclick').one("click", function (e) { $(this).closest('form')[0].submit(); }); }); </script> Commented Jul 19, 2016 at 12:11

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.