转载:http://hi.baidu.com/yangzelai/blog/item/df21393aff7b85feb211c7a0.html
我们有时候希望回车键敲在文本框(input element)里来提交表单(form),但有时候又不希望如此。比如搜索行为,希望输入完关键词之后直接按回车键立即提交表单,而有些复杂表单,可能 要避免回车键误操作在未完成表单填写的时候就触发了表单提交。
要控制这些行为,不需要借助JS,浏览器已经帮我们做了这些处理,这里总结几条规则:
<form action=""> <input type="text"> <input type="button" value="提交"> </form> <h2>一个文本框的时候怎么才能做到不提交,方法是加一个隐藏掉的文本框</h2> <form action=""> <input type="text"> <input type="text" style="display:none"> <input type="button" value="提交"> </form> <h2>只要有type为submit的按钮存在,一个文本框还是多个文本框都提交</h2> <form action=""> <input type="text"> <input type="submit" value="提交"> </form> <h2>只要有type为submit的按钮存在,一个文本框还是多个文本框都提交</h2> <form action=""> <input type="text"> <input type="text"> <input type="submit" value="提交"> </form> <h2>多个文本框的时候,不提交,用type为button的按钮就行啦</h2> <form action=""> <input type="text"> <input type="text"> <input type="button" value="提交"> </form> <h2>用button元素时,FX和IE下有不同的表现</h2> <form action=""> <input type="text"> <input type="text"> <button>提交</button> </form> <h2>radio和checkbox在FX下也会触发提交表单,在IE下不会</h2> <form action=""> <input type="text"> <input type="radio" name="a"> <input type="checkbox" name="b"> <input type="checkbox" name="c"> <input type="button" value="提交"> </form> <h2>type为image的按钮,等同于type为submit的效果</h2> <form action=""> <input type="text"> <input type="text"> <input type="image" src="xxx.gif"> </form> |