chobo2
chobo2

Reputation: 85775

Return False Never Works For me With Jquery Submit? What am I doing wrong?

I am trying to stop my form from submitting yet I never can get it work and always end up changing it to a button type. However I want to see if I keep the submit button as is if I can have in my Asp.net mvc application as a FormCollection Paramater.

Yet I can't even try really passing in anything until I get it to stop submitting. I don't know why I can't get it work

View

$(function()
{
    $("#Submit1").submit(function(e)
    {
        $.post("Page1", function(e)
        {
            alert("alert");
        });

        return false;

    });

});


<% using (Html.BeginForm("Page1","Home",FormMethod.Post,new {@id = "mytest" }))

   { %>
      <%= Html.TextBox("test","hi") %>
      <input id="Submit1" type="submit" value="submit" />
 <% } %>

Controller Method

public bool Page1()
{
    return false;
}

Upvotes: 1

Views: 609

Answers (4)

localshred
localshred

Reputation: 2233

You can always use preventDefault() on the event:

$("#submit-btn").click(function(e){
    e.preventDefault();
    ....
    if (validations pass)
        $("#theform").submit();
});

Upvotes: 0

Darko
Darko

Reputation: 38860

The submit() method should be attached to the form not the submit button. Like this:

$("#mytest").submit(function(e) ....

Upvotes: 0

JeremyWeir
JeremyWeir

Reputation: 24368

I think you want

$("#mytest").submit(function(e)

Upvotes: 0

seth
seth

Reputation: 37267

You need to put the submit on the form, not on the input node.

 $("#mytest").submit(function(e) {
    ...
 }

Upvotes: 3

Related Questions