js獲取select默認選中的Option并不是當前選中值
更新時間:2014年05月07日 09:13:37 作者:
這篇文章主要介紹了js如何獲取select默認選中的Option并不是當前選中的值,需要的朋友可以參考下
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>
不知道還有沒有更方便快捷的方法,曾嘗試通過jQuery獲取$('#sel option[defaultSelected]'),可一直返回空。
各位園友,我要的是select控件初始化的值,非select當前選中的值,初始化的值不隨select值改變,大家可以做一下Demo,當select值改變后,初始化的值是不會變的。
復制代碼 代碼如下:
<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值改變后,初始化的值是不會變的。
您可能感興趣的文章:
- javascript Select標記中options操作方法集合
- js 操作select和option常用代碼整理
- JS & JQuery 動態(tài)添加 select option
- js select option對象小結(jié)
- JS獲取select-option-text_value的方法
- javascript對select標簽的控制(option選項/select)
- JS更改select內(nèi)option屬性的方法
- javascript據(jù)option的value值快速設(shè)定初始的selected選項
- js添加select下默認的option的value和text的方法
- js獲取select選中的option的text示例代碼
- JS實現(xiàn)select選中option觸發(fā)事件操作示例
相關(guān)文章
ES2020讓代碼更優(yōu)美的運算符 (?.) (??)
這篇文章主要介紹了ES2020讓代碼更優(yōu)美的運算符 (?.) (??),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
JS數(shù)組進階示例【數(shù)組的幾種函數(shù)用法】
這篇文章主要介紹了JS數(shù)組進階,結(jié)合實例形式總結(jié)分析了數(shù)組的幾種常見函數(shù)基本用法,涉及JavaScript數(shù)組元素刪除、拼接、添加、倒序排列等相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
用表格輸出1-1000之間的數(shù)字實現(xiàn)代碼(附特效)
本文將介紹下用表格輸出1-1000之間的數(shù)字同時附有特效,感興趣的朋友可以參考下哈,希望對你有所幫助2013-04-04
A標簽中通過href和onclick傳遞的this對象實現(xiàn)思路
想傳遞當前對象給一個函數(shù),于是就將這個URL寫成"Javascript:shoControlSidebar(this)",可是結(jié)果發(fā)現(xiàn)這并不可行,接下來為大家詳細介紹下解決方法2013-04-04

