Using HTML5 validation...
In HTML5 browsers, validation occurs before the submit
event. So if the form is invalid, the submit event never fires.
I would like to hook an event into the form submit, to fire whether the form validates or not. Here's a small example, where I'm trying to alert()
when the user submits the form.
HTML:
<!DOCTYPE html>
<html>
<head><title>Example</title></head>
<body>
<form>
<input type="text" name="foo" required title="Foo field"/>
<input type="submit"/>
</form>
</body>
</html>
JavaScript:
$(function() {
$('form').submit(function() {
alert('submit!')
});
});
Interactive demo: http://jsfiddle.net/gCBbR/
My question is: do browsers provide an alternative event that I can bind to that will run before the validation?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…