Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript function before button submit

I have to call a js function before asp-action is called on button click

<form asp-action="Index" method="post">
                @Html.AntiForgeryToken()
              var x = jsfunction();
                <div class="form-group">
                    <input type="submit" value=@Localizer["Button"] class="btn Button"  />
                </div>
            </form>

I want to call the js funtion on click of the submit button and then it should proceed to the controller action method

like image 708
avg_bloke Avatar asked Dec 06 '25 05:12

avg_bloke


1 Answers

You can try as follow :

<form id='myForm' asp-action="Index" method="post">
            @Html.AntiForgeryToken()
          var x = jsfunction();
            <div class="form-group">
                <input id='submitBTN' type="button" value=@Localizer["Button"] class="btn Button"  />
            </div>
        </form>

And call it's click event by jquery or js"

<script>
  $('#submitBTN').on('click', function () {
    jsfunction();
    $('#myForm').submit();
   });
</script>
like image 149
Majid821 Avatar answered Dec 08 '25 20:12

Majid821