Js function method:
Copy codeThe Code is as follows:
<Script>
Function getdefadefaselectedoption (selectId, valIfNull ){
Var dom, selectId = selectId. replace (/^ #/, ''), opts;
Try {
Opts = document. getElementById (selectId). getElementsByTagName ('option ');
For (var I in opts ){
If (opts [I]. defaultSelected ){
Dom = opts [I];
Break;
}
}
} Catch (e ){
}
Return dom | valIfNull;
}
</Script>
Demo:
Copy codeThe Code is as follows:
<Body>
<Select id = "sel">
<Option value = "1"> 1 </option>
<Option value = "2" selected = ""> 2 </option>
<Option value = "3"> 3 </option>
</Select>
<Button id = "btn"> test </button>
<Script>
Function getdefadefaselectedoption (selectId, valIfNull ){
Var dom, selectId = selectId. replace (/^ #/, ''), opts;
Try {
Opts = document. getElementById (selectId). getElementsByTagName ('option ');
For (var I in opts ){
If (opts [I]. defaultSelected ){
Dom = opts [I];
Break;
}
}
} Catch (e ){
}
Return dom | valIfNull;
}
</Script>
<Script>
Document. getElementById ('btn '). onclick = function (){
Alert (getdefaselecselectedoption ('sel1', {})). value );
};
</Script>
</Body>
I don't know whether there are any more convenient and convenient methods. I tried to get $ ('# sel option [defaultSelected]') through jQuery and can always return null.
Dear friends, what I want is the initial value of the select control, which is not the selected value of the select statement. The initial value does not change with the selected value. You can perform a Demo. When the select value changes, the initial value will not change.