Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 241
user2067567 Avatar asked Apr 11 '13 08:04

user2067567


People also ask

How to Submit form using jQuery in mvc?

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

What is form submit in jQuery?

jQuery submit() MethodThe submit event occurs when a form is submitted. This event can only be used on <form> elements. The submit() method triggers the submit event, or attaches a function to run when a submit event occurs.


2 Answers

$("#test").click(function(){
   $("#sampleForm").submit();
});
like image 130
Deepu Avatar answered Oct 20 '22 01:10

Deepu


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

like image 26
Zach Avatar answered Oct 19 '22 23:10

Zach