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)文章
JavaScript實(shí)現(xiàn)手風(fēng)琴效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)手風(fēng)琴效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02
ES2020讓代碼更優(yōu)美的運(yùn)算符 (?.) (??)
這篇文章主要介紹了ES2020讓代碼更優(yōu)美的運(yùn)算符 (?.) (??),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器效果【推薦】
下面小編就為大家?guī)?lái)一篇javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器效果【推薦】。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-04-04
JS數(shù)組進(jìn)階示例【數(shù)組的幾種函數(shù)用法】
這篇文章主要介紹了JS數(shù)組進(jìn)階,結(jié)合實(shí)例形式總結(jié)分析了數(shù)組的幾種常見(jiàn)函數(shù)基本用法,涉及JavaScript數(shù)組元素刪除、拼接、添加、倒序排列等相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
用表格輸出1-1000之間的數(shù)字實(shí)現(xiàn)代碼(附特效)
本文將介紹下用表格輸出1-1000之間的數(shù)字同時(shí)附有特效,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-04-04
Svelte反應(yīng)式變量和函數(shù)工作原理詳解
這篇文章主要為大家介紹了Svelte反應(yīng)式變量和函數(shù)工作原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
A標(biāo)簽中通過(guò)href和onclick傳遞的this對(duì)象實(shí)現(xiàn)思路
想傳遞當(dāng)前對(duì)象給一個(gè)函數(shù),于是就將這個(gè)URL寫(xiě)成"Javascript:shoControlSidebar(this)",可是結(jié)果發(fā)現(xiàn)這并不可行,接下來(lái)為大家詳細(xì)介紹下解決方法2013-04-04

