admin管理员组文章数量:1428490
This is really strange:
$('form').on('submit', function(event) {
console.log('submit > this=%o', this);
});
Will not show any console message when the form is submitted, but this:
$('form').on('submit', function(event) {
console.log('submit > this=%o', this);
event.preventDefault();
});
Will show it. Why is that? The code must get executed in any case, so that the preventDefault
has any effect!
I have only tested in Chrome, and I would say this is a Chrome bug. Or am I missing sth here?
This is really strange:
$('form').on('submit', function(event) {
console.log('submit > this=%o', this);
});
Will not show any console message when the form is submitted, but this:
$('form').on('submit', function(event) {
console.log('submit > this=%o', this);
event.preventDefault();
});
Will show it. Why is that? The code must get executed in any case, so that the preventDefault
has any effect!
I have only tested in Chrome, and I would say this is a Chrome bug. Or am I missing sth here?
Share Improve this question asked Nov 9, 2015 at 1:18 blueFastblueFast 44.7k66 gold badges214 silver badges374 bronze badges 3- This might be a security feature, interesting. – Olavi Sau Commented Nov 9, 2015 at 1:21
- 1 activate the 'permanent'-option(preserve log) of the console. When you submit the form you leave the document and the console will be cleared by default – Dr.Molle Commented Nov 9, 2015 at 1:29
- @Dr.Molle: oooops! It was driving me crazy, so much that I oversaw this little detail! – blueFast Commented Nov 9, 2015 at 1:39
2 Answers
Reset to default 7The code actually works, the problem is that the console is being cleared immediately because the page was reloaded due to the form submission.
The code event.preventDefault();
has prevented the form from being submitted. This is why the console is not cleared and you are able to see the text being logged.
Works as expected here :)
$('form').on('submit',function(e){
console.log('hell',this)
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="http://www.google." method="POST">
Field:<br>
<input type="text" name="firstname">
<input type="submit" name="submit">
</form>
版权声明:本文标题:javascript - console.log not executed in on('submit') if event.preventDefault() is not done - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745467967a2659616.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论