I am trying to submit a form when I select an option. Without refreshing the page.
Example 1:
<form>
<select>
<option>Option 1</option>
<option>Option 2</option>
</select>
<button type="submit">Submit</button>
</form>
.
$('select').change(function() {
console.log('changed');
this.form.submit(function (e) {
alert('form submitted');
e.preventDefault();
});
});
Demo: https://jsfiddle.net/ty8Lrdm0/
Question: Why is formit still refreshing the page when submitted?
Example 2:
<form id="form">
<select>
<option>Option 1</option>
<option>Option 2</option>
</select>
<button type="submit">Submit</button>
</form>
.
$('select').change(function() {
console.log('changed');
$('#form').submit(function (e) {
alert('form submitted');
e.preventDefault();
});
});
Demo: https://jsfiddle.net/wsjLx1cs/2/
Question: Why does this example not work?
source
share