js函数方法(取得select option值):
复制代码 代码示例:
<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>
例子:
复制代码 代码示例:
<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]'),可一直返回空。
jquery select 设置默认选中
代码:
其中<%=selectedvalue %>为从后台获取的值
复制代码 代码示例:
$(document).ready(function(){
$("#seltype").attr("value",parseInt('<%=selectedvalue %>'));
})