在Javascript中检索表单的"name"属性,而存在名称为"name"的输入

Fré*_*ric 5 javascript forms attributes dom

我有类似这样的HTML结构:

   <form name="myvalue" id="hello">
      <input type="text" name="name" />
    </form>
Run Code Online (Sandbox Code Playgroud)

我想通过跨浏览器解决方案在Javascript中检索表单的name属性.

明显,

document.getElementById("hello").name 
Run Code Online (Sandbox Code Playgroud)

将无法工作,因为它将返回相应的输入对象.

在chrome下,以下代码可以正常工作,但我没有成功找到Internet Explorer 8的等效代码

document.getElementById("hello").getAttribute("name")
Run Code Online (Sandbox Code Playgroud)

提前致谢 !

弗雷德里克

lin*_*lnk 10

我认为这应该有效

document.getElementById("hello").attributes["name"].value;
Run Code Online (Sandbox Code Playgroud)

在IE8中测试确定,这就是我所拥有的.您可能需要进行一些浏览器检查并根据需要选择您的方法.

编辑:实际上,你的例子在IE8中也适合我.但不是ie7.