form 元素内的字段 name 不要跟 form 属性名称一致
长话短说,看这个 form 元素:
<form method="post" action="_some_action_uri_" id="_form_id_">
<input type="hidden" name="method" value="1" />
</form>
试想一下,使用 document.getElementById('_form_id_').getAttribute('method') 会出现什么情况。Firefox 3, Safari 3, Opera 9.5 都会得到预期 “post”, 但是IE 6 和 7 就没有那么幸运了,得到的是一个 object: 其实就是 <input type="hidden" name="method" value="1" /> 这个元素。
因此,为避免混淆和挽救IE,最好是,as the title.
October 8th, 2008 at 22:37
请问在ff3 safari3 opera9.5里面 需要获得form里面的method
规范的方法是怎么做的
我之前是document.getElementById(‘_form_id_’).method
还有其它的么 不要用遍历所有子元素的
October 8th, 2008 at 22:49
@netwjx 正如其名,
getAttribute是用来获取属性值的,直接使用form.method或form['method']方法取的是表单里的字段对象。但是由于浏览器也能够通过form.method或form['method']取得元素的属性值,因此造成混乱。初步怀疑在 IE 里它是不区分这两种方法的,才导致它出现本文中的问题。October 9th, 2008 at 08:42
一直没注意,下次的注意。
October 9th, 2008 at 13:11
ie的form有个elements集合
October 9th, 2008 at 15:06
小细节应该关心,很不错。
@netwjx 其他浏览器也同样支持elements。
October 9th, 2008 at 16:21
又是ie的不规范。
October 9th, 2008 at 20:41
id也是一样,呵呵
另外getAttribute方法默认还不区分大小写
November 7th, 2008 at 18:43
IE的getAttribute方法支持第二个参数,可以设定区分大小写的。
November 7th, 2008 at 18:44
我的留言怎么没发出来?是不是贴的链接太多,当我spam了啊??
November 7th, 2008 at 19:00
没办法,看我的blog吧:http://hax.javaeye.com/blog/264164
November 12th, 2008 at 11:49
John Resig在最新的文章中也提到了这个问题:
http://ejohn.org/blog/deadly-expandos/
December 4th, 2008 at 09:37
最近也发现个怪事。
除了Firefox可以把数据提交到本页,Opera,IE,都不能将提交到本页(从页面的出错信息看出来的)。
最后只有把提交按钮改回原始状态
这样就行了。
December 4th, 2008 at 09:41
)-: 代码被过滤,再贴。
《form action=“this。php” method=“post”》
《input type=“image” src=“go。gif” 、》
《/form》
这样无法提交到本页。
把提交按钮改成原始状态
《input type=“submit” value=“提交” /》
就可以了。
December 16th, 2008 at 15:31
你怎么知道 object 就是 input?
January 6th, 2009 at 09:41
相比楼主,我的遭遇显然要悲惨很多:公司的java组用structs2开发框架,然后根据某某某structs的规定需要在每个form里面都有这么一个隐藏域<input type=”hidden” name=”id” value=”" />…额,你知道我有多无语。
October 25th, 2009 at 21:25
…hax总结的牛