Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JQuery to submit form

I have the following form on a page:

<form action="/" method="post">
<select id="SelectedMonth" name="SelectedMonth">
<option>7/1/2017</option>
<option>6/1/2017</option>
</select>
</form>  

I am trying to use the following jquery snippet to submit the form. The jquery code resides outside of the form and the form is the only form on the page.

 $("#SelectedMonth").change(function () {
         alert(this.value);
         $('form').submit(function (event) {
         alert("Submitted");
         event.preventDefault();
         });
    });

The first alert is triggered and shows the selected value but the submit is never triggered.

It seems like this is pretty straight forward but it is not working. What am I missing?

TIA

like image 356
John S Avatar asked Apr 10 '26 22:04

John S


1 Answers

change action and make it blank and change function like this

$("#SelectedMonth").change(function () {
     $('form').submit();
});

For multiple forms use id

<form id="myForm"><input type="text"/></form>
$("#SelectedMonth").change(function () {
     $('form#myForm').submit();
});

and it will work

like image 174
DEarTh Avatar answered Apr 13 '26 11:04

DEarTh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!