When option element is not set value in Ie6/7/8, select will get an empty string _javascript tips

Source: Internet
Author: User
As follows
Copy Code code as follows:

<! DOCTYPE html>
Select will get an empty string </title> when option element is not set value in <TITLE>IE6/7/8
<body>
<select onchange= "alert (this.value)" >
<option>one</option>
<option>two</option>
<option>three</option>
</select>
</body>

When the change event is triggered, the results of the test in each browser are as follows:
IE6/7/8 : Pop-up empty string
Ie9/firefox/safari/chrome/opera : POPs the innertext value of the corresponding OPTION element.

It is obvious that the realization of ie9/firefox/safari/chrome/opera has some reason. That is, the value of option and option innertext can be omitted at the same time. This is more concise. Unfortunately, IE6/7/8 does not support this writing. To ensure compatibility with all browsers, be sure to write option with the value attribute missing.

Make a slight change to the HTML code above

Copy Code code as follows:

<select onchange= "alert (this.value)" >
<option value= "1" >one</option>
<option>two</option>
<option>three</option>
</select>

Added the Value property to the first option. Then the test steps are as follows
1, choose Two
2, select one

The results of the two-time pop-up are as follows:
IE6/7/8: [Empty string, 1]
Ie9/firefox/safari/chrome/opera: [two,1]

From the results you can see that the implementation of the browser is probably as follows:

In IE6/7/8, if option does not have a value, an empty string is returned.
In Ie9/firefox/safari/chrome/opera, the value of option is taken first, and if there is no value attribute, the innertext of option is taken.

And then modify the code
Copy Code code as follows:

<select onchange= "alert (this.value.length)" >
<option value= "1" >one</option>
<option> two </option>
<option>three</option>
</select>

Compared to the previous step, the second option two with spaces on either side. This time we alert the length of value. Select Two. At this time the browser popped the results as follows
IE6/7/8 : 0
Ie9/firefox/safari/chrome/opera : 3

IE6/7/8 returns an empty string for option without the value attribute, whose length is naturally 0. The main focus of this test is ie9/firefox/safari/chrome/opera these modern browsers. All of them return 3 but not 5. You can see the inside of these browsers remove the two on both sides of the space (trim).

The previous test has mentioned that the option Value,value attribute in Ie9/firefox/safari/chrome/opera has no innertext value for option. For option that does not have the Value property set, they try to return their innertext as value and even automatically remove the whitespace on both sides.

The above mentioned innertext that return option is not innerhtml. And then modify the code

Copy Code code as follows:

<select onchange= "alert (this.value)" >
<option value= "1" >one</option>
<option><span>two</span></option>
<option>three</option>
</select>

The second option has no value attribute, which is a SPAN element. Then choose Two. It was still "two", not "<span>two</span>", that popped in Ie9/firefox/safari/chrome/opera. If alert out of its length it will find that it is still 3, rather than the length of "<span>two</span>" 16.

You can see that when you forget to write option value, these modern browsers will try to return the correct (client programmer wants) result value, which is more fault tolerant than IE6/7/8 do.

Related:
Differences in the performance of option elements in browsers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.