为什么在onsubmit返回undefined后提交表单?

wcd*_*wcd 4 html javascript forms

我只在Chrome和Firefox中测试过.

<form method="POST" action="post.php?action=newthread&fid=170" onsubmit="return validate(this)">
    <!--some other input elements-->
    <input type="submit">
</form>

<script>
    function validate(form) {
        // do some check, if failed, returns false
        // does not have return statement, returns undefined in Chrome and Firefox
    }
</script>
Run Code Online (Sandbox Code Playgroud)

事实证明,如果函数validate返回undefined,表单仍然会被提交.但如果它返回false,表单将不会被提交,这是预期的.我以为undefined会阻止提交表单.有人可以解释这种行为吗?谢谢.

Bar*_*mar 6

JavaScript使用精确的比较(如===而不是==)测试的事件处理程序的返回值时.阻止默认操作的唯一返回值是false.任何其他返回值(包括undefined)都允许执行默认操作.