js函数方法:
<script>
function getDefaultSelectedOption(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:
<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 getDefaultSelectedOption(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((getDefaultSelectedOption(\'sel1\', {})).value);
};
</script>
</body>
不知道还有没有更方便快捷的方法,曾尝试通过jQuery获取$(\'#sel option[defaultSelected]\'),可一直返回空。
各位园友,我要的是select控件初始化的值,非select当前选中的值,初始化的值不随select值改变,大家可以做一下Demo,当select值改变后,初始化的值是不会变的。
本文地址:https://www.stayed.cn/item/13058
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我