Firefox不会验证表单数据

时间:2022-11-23 17:33:06

The following simple form with javascript with onclick button does not work in Firefox (I have version 7) but works fine in IE, Chrome, and Safari. What am I missing?

下面带有带onclick按钮的javascript的简单表单在Firefox中不起作用(我有版本7)但在IE,Chrome和Safari中工作正常。我错过了什么?

<head>
<script type="text/javascript">
    function result() {
        alert(calc.input.value);
        }
</script>

</head>
<body>

<form name="calc" action="">
    <input class="cInput" type="text" name="input" size="16" /><br/>
    <input type="button" class="cButton" name="seven" value="1" onclick="calc.input.value += '1'" />
    <input type="button" class="cButton" name="equal" value="=" onclick="result()" /><br/>
</form>
</body>

2 个解决方案

#1


1  

<script type="text/javascript">
    var calc;
    function result() {
        calc = document.forms['calc'];
        alert(calc.input.value);
        }
</script>

#2


1  

What am I missing?

我错过了什么?

You are missing that IE adds element names and ids as global variables. Some other browsers in certain conditions (sometimes requiring the document to be in quirks mode) will copy that habbit, some wont.

您缺少IE将元素名称和ID添加为全局变量。某些条件下的某些其他浏览器(有时需要文档处于怪癖模式)将复制该habbit,有些则不会。

So in IE (and some others) the form name calc is a global variable referencing the form and in other browsers it isn't.

所以在IE(和其他一些)中,表单名称calc是引用表单的全局变量,而在其他浏览器中则不是。

The method in Dunuyadnd's answer is a robust, cross-browser way to get a reference to the form. The formal access method is:

Dunuyadnd的答案中的方法是一种强大的跨浏览器方式来获取对表单的引用。正式的访问方法是:

document.forms['calc'];

Named form controls can accessed similarly:

命名表单控件可以类似地访问:

document.forms['calc'].elements['input'];

or

要么

document.calc.input;

Be careful with giving controls names that are the same as tags, it is a bit confusing. Also, if there is more than one control with the same name, you will get an HTML collection rather than a single element.

提供与标签相同的控件名称时要小心,这有点令人困惑。此外,如果有多个具有相同名称的控件,您将获得HTML集合而不是单个元素。

You may want to read about HTMLForms and their related elements.

您可能想要阅读有关HTMLForms及其相关元素的信息。

#1


1  

<script type="text/javascript">
    var calc;
    function result() {
        calc = document.forms['calc'];
        alert(calc.input.value);
        }
</script>

#2


1  

What am I missing?

我错过了什么?

You are missing that IE adds element names and ids as global variables. Some other browsers in certain conditions (sometimes requiring the document to be in quirks mode) will copy that habbit, some wont.

您缺少IE将元素名称和ID添加为全局变量。某些条件下的某些其他浏览器(有时需要文档处于怪癖模式)将复制该habbit,有些则不会。

So in IE (and some others) the form name calc is a global variable referencing the form and in other browsers it isn't.

所以在IE(和其他一些)中,表单名称calc是引用表单的全局变量,而在其他浏览器中则不是。

The method in Dunuyadnd's answer is a robust, cross-browser way to get a reference to the form. The formal access method is:

Dunuyadnd的答案中的方法是一种强大的跨浏览器方式来获取对表单的引用。正式的访问方法是:

document.forms['calc'];

Named form controls can accessed similarly:

命名表单控件可以类似地访问:

document.forms['calc'].elements['input'];

or

要么

document.calc.input;

Be careful with giving controls names that are the same as tags, it is a bit confusing. Also, if there is more than one control with the same name, you will get an HTML collection rather than a single element.

提供与标签相同的控件名称时要小心,这有点令人困惑。此外,如果有多个具有相同名称的控件,您将获得HTML集合而不是单个元素。

You may want to read about HTMLForms and their related elements.

您可能想要阅读有关HTMLForms及其相关元素的信息。