I want to submit a form when changing a drop down menu and want to prevent the page from reloading.
<script src="jquery-1.9.1.min.js"></script> $(document).ready( function(){ $('#adv_search').submit( function(e){ alert('form submitted'); e.preventDefault(); }); });
<form id="adv_search" name="adv_search" method="post"> <select name="state" id="state" onchange="document.adv_search.submit()"> ------ </select> </form>
The plain javascript's submit()
will trigger the natural submit process of a form, you cannot bypass it with a jquery's submit
. So it is better to use jQuery alone for doing this,
$(document).ready(function() {
$("#state").change(function() {
$('#adv_search').submit();
});
$('#adv_search').submit(function(e) {
alert('form submitted');
e.preventDefault();
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.