Set the default select option in JavaScript.
You can use selectobj. Options [Index]. Selected = true; To set
Selectobj drop-down box object
Index subscript of the default item
If the default subscript cannot be determined for the number of options, you can use the following method.
This method also returns the subscript of this option after setting the default option.
// Set the default select option and obtain the subscript of the option.
// Parameter 1: the drop-down box object; parameter 2: The text content of the option
Function setselected (selectobj, optionstext ){
For (VAR I = 0; I <selectobj. Options. length; I ++ ){
If (selectobj. Options [I]. Text = optionstext ){
Selectobj. Options [I]. Selected = true;
Return I;
}
}
}
Of course, this method can also be changed to the selected value to determine the default options.
Example:
<Select id = "seletest" name = "">
<Option value = "0"> A </option>
<Option value = "1"> B </option>
<Option value = "2"> C </option>
<Option value = "3"> d </option>
<Option value = "4"> E </option>
</SELECT>
<SCRIPT>
VaR selobj = Document. getelementbyid ('seletest ');
VaR S2 = setselected (selobj, 'D ')
Alert ('s2 = '+ S2 );
</SCRIPT>