js函数方法:
<script> <BR>function getDefaultSelectedOption(selectId, valIfNull) { <BR>var dom, selectId = selectId.replace(/^#/, ''), opts; <BR>try { <BR>opts = document.getElementById(selectId).getElementsByTagName('option'); <BR>for (var i in opts) { <BR>if (opts[i].defaultSelected) { <BR>dom = opts[i]; <BR>break; <BR>} <BR>} <BR>} catch (e) { <BR>} <BR>return dom||valIfNull; <BR>} <BR></script>
Demo:
<script> <BR>function getDefaultSelectedOption(selectId, valIfNull) { <BR>var dom, selectId = selectId.replace(/^#/, ''), opts; <BR>try { <BR>opts = document.getElementById(selectId).getElementsByTagName('option'); <BR>for (var i in opts) { <BR>if (opts[i].defaultSelected) { <BR>dom = opts[i]; <BR>break; <BR>} <BR>} <BR>} catch (e) { <BR>} <BR>return dom||valIfNull; <BR>} <BR></script>
<script> <BR>document.getElementById('btn').onclick = function () { <BR>alert((getDefaultSelectedOption('sel1', {})).value); <BR>}; <BR></script>
不知道还有没有更方便快捷的方法,曾尝试通过jQuery获取$('#sel option[defaultSelected]'),可一直返回空。
各位园友,我要的是select控件初始化的值,非select当前选中的值,初始化的值不随select值改变,大家可以做一下Demo,当select值改变后,初始化的值是不会变的。