RollerCosta
RollerCosta

Reputation: 5196

jQuery submit form on linkClick event

Form
@using (Html.BeginForm(new { id="form"})) { //fields}

Link

<div align="center" id="submitLink">@Html.ActionLink("Next", "address", "PES", new { abc= "xyz" }, null)</div>



Script

$(function () {
    $("#submitLink").click(function () { $("#form").submit(); });
});


Form is not submitting when i click next link
What i am doing wrong??

Upvotes: 0

Views: 571

Answers (3)

RollerCosta
RollerCosta

Reputation: 5196

Make your DOM ready first and as suggested by these guys "provide id to your a tag instead of div"

  $(document).ready(function () {
        $(function () {
            $("#submitLink").click(function () { $("#form").submit(); });
        });
 }); 

Upvotes: 1

Per Kastman
Per Kastman

Reputation: 4504

Try to set the ID on the link instead of the div:

<div align="center">

    @Html.ActionLink("Next", "address", "PES", new { abc= "xyz" },  new { id="submitLink" })

</div>

You should also consider setting an action and if needed a controller to your form:

@using (Html.BeginForm("actionName","controller",FormMethod.Post, new { id="form"}))
{

}

The overload of your BeginForm you're using now is that you set routeValues to id="form".

Upvotes: 1

Gaurav Agrawal
Gaurav Agrawal

Reputation: 4431

Use this script instead of your script and check it now it works or not... please provide submitLink id to anchor tag....

$.document.ready(function () {
    $("#submitLink").attr("onclick", "$('#form').submit();");
    $("#submitLink").attr("href","javascript:void(0);");
});

I think this concept will works.....

Upvotes: 0

Related Questions