Document directory
Select tags of HTML files
<Select multiple = "true" id = "seldealgoods" class = "selectmul">
</SELECT>
JS Code for adding Select Options
VaR newoption = document. createelement ('option'); <br/> if ($ ("# cherelate") [0]. checked) {<br/> newoption. TEXT = "bidirectional Association --" + selectgoods. options [I]. text; <br/> newoption. value = selectgoods. options [I]. value + "-Two"; <br/>}< br/> else {<br/> newoption. TEXT = "unidirectional Association --" + selectgoods. options [I]. text; <br/> newoption. value = selectgoods. options [I]. value + "-one"; <br/>}< br/> seldealgoods. add (newoption, null); <br/>
Selectgoods
For another select tag
The Code does not seem to have any problems. You can successfully Add the option when running on IE8 and ff. During IE6 testing, seldealgoods. Add (newoption, null );
The prompt type does not match.
Error
In w3school
View the Add Method
Syntax
selectObject.add(option,before)
| Parameters |
Description |
| Option |
Required. The option element to add. It must be an option or optgroup element. |
| Before |
Required. Add a new element before the element in the option array. If this parameter is null, the element is added to the end of the Option array. |
No syntax error. What is the problem? IE8 FF can achieve the effect, and IE6 is not compatible.
View the sample code given by w3cschool
<HTML> <br/> <pead> <br/> <MCE: Script Type = "text/JavaScript"> <! -- <Br/> function insertoption () <br/>{< br/> var y = document. createelement ('option'); <br/> Y. TEXT = 'kiw' <br/> var x = document. getelementbyid ("myselect"); <br/> try <br/>{< br/> X. add (Y, null); // standards compliant <br/>}< br/> catch (Ex) <br/>{< br/> X. add (y); // ie Only <br/>}< br/> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> <form> <br/> <select id = "myselect"> <br/> <option> Apple </option> <br/> <option> pear </option> <br/> <option> banana </option> <br/> <option> orange </option> <br/> </SELECT> <br/> <input type = "button" onclick = "insertoption () "<br/> value =" insert option "/> <br/> </form> <br/> </body> <br/> </ptml>
When calling the add method, you can see that there is a try catch exception capture, and the catch shows that IE uses X. Add (y)
That is to say, ie support
SelectObject. Add (option), so the JS Code is changed
VaR newoption = document. createelement ('option'); if ($ ("# cherelate") [0]. checked) {newoption. TEXT = "bidirectional Association --" + selectgoods. options [I]. text; newoption. value = selectgoods. options [I]. value + "-Two";} else {newoption. TEXT = "unidirectional Association --" + selectgoods. options [I]. text; newoption. value = selectgoods. options [I]. value + "-one";} Try {seldealgoods. add (newoption, null);} catch (Ex) {seldealgoods. add (newoption );}
Running properly