This article focuses on two points:
1. When select has only one or more values, length represents different meanings.
2. How to Make length unique.
Let's take a look at two examples.
Example 1:
<P>
<Select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT>
</P>
<P>
<Button name = "B3" onclick = "alert (d1.length)"> click me </button>
</P>
Example 2:
<P>
<Select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT>
<Select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT> </P>
<P>
<Button name = "B3" onclick = "alert (d1.length)"> click me </button> </P>
Click the two buttons in Example 1 and Example 2. Alert has two different results: Example 1 is 3, Example 2 is 2, and d1.length is the same. Why?
This is what I want to talk about:
1. Length indicates different meanings.
When select has only one option, its length indicates the number of options.
When multiple select statements exist, its length indicates the length of the select array.
2. How to make it unique?
Let's make some changes to the two examples above:
Example 1:
<P> <select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT> <p>
<P> <button name = "B3" onclick = "alert (document. getelementsbyname ('d1 '). Length)"> click me </button> </P>
Example 2:
<P> <select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT> <select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT> </P>
<P> <button name = "B3" onclick = "alert (document. getelementsbyname ('d1 '). Length)"> click me </button> </P>
Now let's try again and see if the length points to the select length?
So how to obtain the number of options?
Write as follows:
Oele = Document. getelementsbyname ('1 ')
Optionscount = oele [0]. Options. Length
Conclusion:
When we cannot predict the number of select statements with the same name or ID, we use the document. getelementsbyname (). length method to obtain the select length.