onsubmit return false仍提交表单

博主之前遇到这样的问题,是因为代码有错,改正之后就正常了。

但今天确定代码没错,仍然return false提交表单。

总结网上各路大神的解释:

1.onsubmit的作用是防止form只有一个input时提交表单

2.onsubmit的作用是先提交表单的动作再执行函数,最后无论函数返回正确与否都完成提交动作

具体真相读者自行百度吧。我偏向2,但与我之前的情况相悖,也不完全确定。

做法:

只能麻烦一点将input submit换成input button了。fun里设置表单提交,不用多解释了吧

但是,<button id="submit" onclick="check();">下一题</button>无论funciton里怎么写代码仍然提交

表单,再次查询得知button只有IE才默认是button,其他浏览器和W3C规范都默认是submit

所以正确做法是:<button id="submit" type="button" onclick="check();">下一题</button>

但问题再次出现,form不提交了。再次查询原因,发现这篇博文遇到了同样的问题。原文:

点击提交按钮出现确认提示,但是确认后就没反应了

但是将button的type改为submit却又能提交

于是上jQuery API查找原因,看到以下这段文字顿时明白了:

Additional Notes:

Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submitlength, or method. Name conflicts can cause confusing failures. For a complete list of rules and to check your markup for these problems, see DOMLint.

大概意思是表单和其子元素不宜用一个表单的属性的属性作为name或id的名称,如submit, length, 和 method等,否则会产生冲突,名称冲突可能就会导致这种情况。

对号入座,我确实将button的id设置了submit,改后就好了

原文地址:https://www.cnblogs.com/yanan7890/p/8949818.html