I have the following code:
<form>
<input type="text" name="keyword" value="keyword">
<input type="submit" value="Search">
</form>
I was wondering if anyone could show me with jquery how I could put an e prevent default on the submit button, so that if the submit button is triggered I can get it to instead do something else other than submit, say have an alert.
So far I have this:
function submit_hit() {
e.preventDefault();
alert('History set');
}
$('form input:submit').on('click', function(e) {
e.preventDefault();
alert('History set');
});
You can use this:
$('form').on('submit',function(e){
e.preventDefault();
alert('Prevented!');
});
This function will prevent the form submit. It will fire when the form is submited. With click
or with Enter
key.
If you have many forms can be good to use a ID on this selector, like $('#form_id').on('submit',function(e){//...ect
Demo here
$('form input[type=submit]').on('click',function(e){
e.preventDefault();
alert('Prevented!');
});
This function will also prevent the form submit. It will fire when the input button is clicked
.
Demo here
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.