给定下面的表单
:
<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/