javascript - 在 IE6 中读取表单操作属性,如果表单有一个名为 "action"的字段

标签 javascript html internet-explorer-6 forms

给定下面的表单:

<form id="myForm" action="index.php">
    <input type="hidden" name="action" value="list" />
    <input type="submit" />
</form>

如何使用 IE6 获取表单 (index.php) 的 action 属性的值?

提示:我试过了

document.getElementById('myForm').action

document.getElementById('myForm').getAttribute('action')

但是它们都不起作用。它们都返回 input 字段 (document.getElementById('myForm').action.value == 'list')。

最佳答案

有一个简单的方法,使用节点的attributes收藏:

document.getElementById("myForm").attributes["action"].value

测试页面进行尝试(也证明了 getAttribute 的缺陷,如 David Dorward 的回答所述):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Form Action Access</title>
<script type="text/javascript">
function dotAccess()
{
    alert(document.getElementById("myForm").action.value);
}
function getAttributeAccess()
{
    alert(document.getElementById("myForm").getAttribute("action"));
}
function attributesAccess()
{
    alert(document.getElementById("myForm").attributes["action"].value);
}
</script>
</head>
<body>
<form id="myForm" action="foo">
  <input type="hidden" name="action" value="bar">
  <input type="button" onclick="dotAccess()" value="form.action">
  <input type="button" onclick="getAttributeAccess()" value='form.getAttribute("action")'>
  <input type="button" onclick="attributesAccess()" value='form.attributes["action"]'>
</form>
</body>
</html>

关于javascript - 在 IE6 中读取表单操作属性,如果表单有一个名为 "action"的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1378155/

相关文章:

javascript - 延迟链接点击

javascript - Async + ClickBusting + Cordova + Android Galaxy S4 = 意外退出

javascript - 在文档中查找、更新和返回数组对象 (mongodb)

css - IE6 中的 3 像素额外空间

javascript - 有什么方法可以快速轻松地将整个网站的标准网站加载转换为 AJAX?

javascript - Javascript、Ajax 来自另一个 php 文件的 HTML 字符集错误

java - 无法使用 document.getElementById 获取元素,返回 null

css - 如何删除下拉菜单列表 <a> 中的填充?

css - Internet Explorer 边框错误

javascript - 窗口位置问题