js獲取select默認(rèn)選中的Option并不是當(dāng)前選中值
更新時(shí)間:2014年05月07日 09:13:37 作者:
這篇文章主要介紹了js如何獲取select默認(rèn)選中的Option并不是當(dāng)前選中的值,需要的朋友可以參考下
js函數(shù)方法:
<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>
不知道還有沒(méi)有更方便快捷的方法,曾嘗試通過(guò)jQuery獲取$('#sel option[defaultSelected]'),可一直返回空。
各位園友,我要的是select控件初始化的值,非select當(dāng)前選中的值,初始化的值不隨select值改變,大家可以做一下Demo,當(dāng)select值改變后,初始化的值是不會(huì)變的。
復(fù)制代碼 代碼如下:
<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:
復(fù)制代碼 代碼如下:
<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>
不知道還有沒(méi)有更方便快捷的方法,曾嘗試通過(guò)jQuery獲取$('#sel option[defaultSelected]'),可一直返回空。
各位園友,我要的是select控件初始化的值,非select當(dāng)前選中的值,初始化的值不隨select值改變,大家可以做一下Demo,當(dāng)select值改變后,初始化的值是不會(huì)變的。
您可能感興趣的文章:
- javascript Select標(biāo)記中options操作方法集合
- js 操作select和option常用代碼整理
- JS & JQuery 動(dòng)態(tài)添加 select option
- js select option對(duì)象小結(jié)
- JS獲取select-option-text_value的方法
- javascript對(duì)select標(biāo)簽的控制(option選項(xiàng)/select)
- JS更改select內(nèi)option屬性的方法
- javascript據(jù)option的value值快速設(shè)定初始的selected選項(xiàng)
- js添加select下默認(rèn)的option的value和text的方法
- js獲取select選中的option的text示例代碼
- JS實(shí)現(xiàn)select選中option觸發(fā)事件操作示例
相關(guān)文章
JS實(shí)現(xiàn)的郵箱提示補(bǔ)全效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)的郵箱提示補(bǔ)全效果,涉及javascript正則匹配、事件響應(yīng)及頁(yè)面元素動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2018-01-01HTML中setCapture、releaseCapture 使用方法淺析
本文給大家簡(jiǎn)單介紹了下html中的高級(jí)拖動(dòng)技術(shù)setCapture、releaseCapture的使用方法,有需要的小伙伴可以參考下2016-09-09bootstrap table 多選框分頁(yè)保留示例代碼
在使用bootstrap table的復(fù)選框功能的時(shí)候,由于采用服務(wù)端分頁(yè),當(dāng)在第一頁(yè)選擇了某些數(shù)據(jù),然后點(diǎn)擊第二頁(yè)選擇一些數(shù)據(jù),再次點(diǎn)回第一頁(yè),發(fā)現(xiàn)原先選擇的數(shù)據(jù)已經(jīng)清空了,原來(lái)的多選框并不支持翻頁(yè)保留多選數(shù)據(jù),怎么解決呢,下面小編給大家分享下解決思路2017-03-03仿服務(wù)器端腳本方式的JS模板實(shí)現(xiàn)方法
仿服務(wù)器端腳本方式的JS模板實(shí)現(xiàn)方法...2007-04-04基于JS實(shí)現(xiàn)移動(dòng)端向左滑動(dòng)出現(xiàn)刪除按鈕功能
最近在做移動(dòng)端項(xiàng)目時(shí),需要實(shí)現(xiàn)一個(gè)列表頁(yè)面的每一項(xiàng)item向左滑動(dòng)時(shí)出現(xiàn)相應(yīng)的刪除按鈕,其實(shí)實(shí)現(xiàn)此功能很簡(jiǎn)單的。這篇文章主要介紹了基于js實(shí)現(xiàn)移動(dòng)端向左滑動(dòng)出現(xiàn)刪除按鈕,需要的朋友可以參考下2017-02-02