js的onsubmit阻止不了jquery的submit事件么

<form onsubmit='console.log(11);return false;'>

    <input type="submit">

</form>

<script>

$("form").submit(function(){

console.log(22);

})

</script>

这样会输出11和22,$("form")的submit放在了公共了暂时不能直接删了这代码,怎样才能只输出11呢?

莫回无
浏览 533回答 1
1回答

月关宝盒

搞定。。<form onsubmit='console.log(11);return false;'>&nbsp; &nbsp; <input type="submit"></form><script>$("form").submit(function(){var onsubmit = $(this).attr('onsubmit');if(typeof onsubmit != 'undefined'){&nbsp; &nbsp; var tempFn = '(function a(){' + onsubmit + '})();',&nbsp; &nbsp; &nbsp; &nbsp; tempFnRs = eval(tempFn);&nbsp; &nbsp; if(tempFnRs == false){&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}console.log('22');})这样可以阻止jquery后续的代码,然后再在onsubmit执行的代码里加个flag判断,让onsubmit里的alert什么的弹出框只执行一次即可,如if(!window.flag){alert('err');window.flag = true;} return false;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript