我不太熟悉如何调试此类问题。在我的 HTML 中,假设我有一个表单:
<form name="myForm" >
<table>
<tr>
<td>
<input type="radio" name="myType" value="val" onclick="someFunc(this.value)"checked > stuff here
<input type="radio" name="myType" value="val2" onclick="someFunc(this.value)"> stuff2 here
</td>
..
</form>
在 Javascript 代码中,我通过以下方式引用它:
myForm.myType[0].checked
在 IE 中这很好用,但在 Firefox 中不行。在 Firefox 中我试过:
alert (myForm)
还有:
if (frmDateType == null)
{
alert('null!');
}
else
{
alert('not null!');
}
然而,在这两种情况下,Firefox 甚至不显示警报,它基本上什么都不做,尽管它在 IE 中很好。我已经安装了 Firebug,它没有显示任何错误(或者至少我找不到它们)。所以我不确定如何调试这样的问题,为什么我没有看到任何错误以及为什么 Firefox 不喜欢 MyForm?有小费吗?
最佳答案
您假设任何具有名称属性的东西都将获得一个匹配的全局变量。这是非标准行为。
无论如何你都不应该给表单命名属性,参见规范:
Note. This attribute has been included for backwards compatibility. Applications should use the id attribute to identify elements.
因此,首先用 id 替换 name 属性。
<form id="myForm">
然后您可以通过以下方式访问表单:
document.getElementById('myForm')
或
document.forms.myForm
关于javascript - 无法在 Firefox 中调用 Form 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8115369/