user2067567
user2067567

Reputation: 3813

Form Submit using Jquery and mvc

Am trying to submit a form using jQuery. My code below

<form id="sampleForm" action="@Url.Action("submitSearch","Home")">
    //Form Fields  textbox
    <button id="test" type="submit" ></button>
</form>

Inside document.ready having script

$("#sampleForm").submit(function () {
    console.log("Success");
});

But when click sumbit nothing is happening.

I tried the below code to see if I have mapped correctly and that works fine

$(document).on("click", "#search-button", function () {
    console.log("Success");
});

What am missing while submitting form ?

Upvotes: 8

Views: 41303

Answers (4)

andres descalzo
andres descalzo

Reputation: 14967

This is the syntax for tag button:

<button type="submit" value="Submit">Submit</button>

if you don't like or don't need put the text of button, and you only need run the event submit of a form, you cant do this:

html:

<form id="sampleForm" action="@Url.Action("submitSearch","Home")" method="get">
    //Form Fields  textbox
</form>

js:

// where needed
$('#sampleForm').submit();
// or
$('#sampleForm').trigger('submit');

but always you need put any tag input for compatibility with IE

Upvotes: 0

user2007801
user2007801

Reputation:

It should be like this

<input id="test" type="submit" ></input>

Instead of this

<button id="test" type="submit" ></button>

Upvotes: 1

Zach
Zach

Reputation: 3207

Is it possible you're submitting but the page is reloading and the console log is cleared?

Also, just to clarify...

$("#sampleForm").submit(function () {
    console.log("Success");
});

This wires up an event to do stuff when the sampleForm is submitted...

$('#sampleForm').submit();

actually triggers the submit event

Upvotes: 4

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

$("#test").click(function(){
   $("#sampleForm").submit();
});

Upvotes: 14

Related Questions