JS函数方法:
复制代码代码如下:
<script>
函数getDefaultSelectedOption(selectid,valifnull){
var dom,selectid = selectid.replace(/^#/,''),opts;
尝试 {
opts = document.getElementById(selectId).getElementsBytagName('option');
for(opts中的var i){
if(opts [i] .defaultselected){
dom = opts [i];
休息;
}
}
}捕获(e){
}
返回dom || valifnull;
}
</script>
演示:
复制代码代码如下:
<身体>
<选择ID =“ SEL”>
<option value =“ 1”> 1 </option>
<option value =“ 2” selected =“”> 2 </option>
<选项值=“ 3”> 3 </option>
</select>
<按钮ID =“ BTN”>测试</button>
<script>
函数getDefaultSelectedOption(selectid,valifnull){
var dom,selectid = selectid.replace(/^#/,''),opts;
尝试 {
opts = document.getElementById(selectId).getElementsBytagName('option');
for(opts中的var i){
if(opts [i] .defaultselected){
dom = opts [i];
休息;
}
}
}捕获(e){
}
返回dom || valifnull;
}
</script>
<script>
document.getElementById('btn')。onclick = function(){
alert(((getDefaultSelectedOption('sel1',{})))。值);
};
</script>
</body>
不知道还有没有更方便快捷的方法,曾尝试通过jquery获取$('#sel option [defaultSelected]'),可一直返回空。
各位园友,我要的是,选择控件初始化的值,非,选择当前选中的值,初始化的值不随,选择值改变,大家可以做一下,大家可以做一下