- 当type=button 和type=submit的区别
案例1:type=button
<script type="text/javascript">
function submit1(){
alert(document.getElementById('result').value);
document.getElementById('result').value = "success";
alert(document.getElementById('result').value);
}
</script>
<form id='form'>
result: <input type='text' id='result'/>
<button type="button" onclick="submit1(); " >提交</button>
</form>
结果:只执行onclick事件
案例2:type=submit
<script type="text/javascript">
function submit1(){
document.getElementById('result').value = "success";
}
</script>
<form id='form'>
result: <input type='text' id='result'/>
<button type="submit" onclick="submit1(); " >提交</button>
</form>
结果:先执行onclick事件,再提交表单
原因:关于button标签的type属性,IE 的默认类型是 “button”,而其他浏览器中(包括 W3C 规范)的默认值是 “submit”,所以应该始终为button标签规定 type 属性。