HTML 版本最近有变化吗(比如从 ie7 到 IE8?) 我注意到以下变化让我有些烦恼 - 我有一个类似于此的代码:
<form method="POST" action="/admin/modify">
<input type="text"/>
<button onclick="dosomething()">Press</button>
</form>
<script type="text/javascript">
function doSomething(){
// doesn't matter what actually
}
</script>
这段代码让我感到奇怪的是,通过按下表单内的按钮,我只想执行一些 javascript 操作,但最终它会导致表单也被提交,即使我不愿意这样做.
所以——是真的吗?如果是这样,我如何在 from 中执行一些 java 脚本 actoin 但防止自动提交表单?
最佳答案
根据 W3schools , submit
是 IE 8 中 button
元素的新默认操作:
Always specify the type attribute for the button. The default type for Internet Explorer is "button", while in other browsers (and in the W3C specification) it is "submit".
因此,如果您不指定类型,则该表单将在所有浏览器中提交,但不会在 IE 7 中提交。
这应该有效:
<button type="button" onclick="dosomething()">Press</button>
关于javascript - 表单中的按钮导致它自动提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2440043/