I. Test Environment
Browser: IE6 +, FF 3.5.5, Opera 10, Chrome 4.0.249, Safari 4.0.3
Ii. Example
Copy codeThe Code is as follows:
<Form name = "test-form" action = "" method = "">
<Input type = "checkbox" name = "kk">
<Form>
<Script type = "text/javascript">
Var oForm = document. forms ['test-form'],
Eles = oForm. elements ['kk '];
Alert (eles. length); // undefined
Alert (eles. nodeType); // 1
</Script>
3. solution (the method I think of is to change the acquisition method, based on YUI)
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var oForm = document. forms ['test-form'],
Eles = YAHOO. util. Dom. getElementsBy (function (el ){
Return el. type = 'checkbox' & el. name = 'kk ';
}, 'Input', oForm );
Alert (eles. length); // 1
</Script>
Document. formname. inputname
I have encountered this problem once before, but I forgot this time and still made another mistake, so I have to record it.
Take a look at this example:
Copy codeThe Code is as follows:
<Form name = "hehe">
<Input type = "checkbox" name = "haha"/>
</Form>
<Form name = "hehe2">
<Input type = "checkbox" name = "haha"/>
<Input type = "checkbox" name = "haha"/>
</Form>
<Script type = "text/javascript">
Document. write (document. hehe. haha. length );
Document. write ('<br/> ');
Document. write (document. hehe2.haha. length );
</Script>
Demo
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <meta http-equiv =" content-Type "content =" text/html; charset = gb2312 "/> <title> untitled document </title> </pead> <body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
I usually get used to this method when getting the input of a form, but this often leads to problems, because in many cases, I will compare whether the number of selected checkboxes is equal to the number of all checkboxes, however, an error occurs when only one object is returned, because it returns the checkbox itself and does not have the length attribute. Therefore, we need to first determine whether the obtained input has the length attribute for further operations.
In addition, the location is recorded. reload (), which also has an optional parameter, true or false. If omitted or set to false, it uses the HTTP header If-Modified-Since to check whether the files on the server have changed. If the document has changed, reload () will download the document again. If the document has not changed, this method will load the document from the cache. This is exactly the same as clicking the refresh button in the browser. If it is set to true, no matter what the last modification date of the document is, it will bypass the cache and re-download the document from the server. Is the legendary force refresh.