js獲取當前select 元素值的代碼
更新時間:2010年04月19日 21:22:37 作者:
js獲取當前select 元素值的代碼,需要的朋友可以參考下。
- 如果 select 元素下的所有 option 元素均沒有指定 selected 屬性,會默認選中第一個。
- 可以通過 select.selectedIndex 獲取到選中的 option 元素的索引。
- 可以通過 select.options[select.selectedIndex] 獲取到選中的 option 元素。
- option 元素 <option selected="selected" value="value3">text3</option>,可以通過 option.value 獲得 option 元素的 value 屬性值,即 value3;可以通過 option.text 獲得 option 元素內(nèi)的文本,即 text3。
- 如果 option 元素沒有定義 value 屬性,則 IE 中 option.value 無法獲得,但 Safari、Opera、FireFox 依舊可以通過 option.value 獲得,值同于 option.text 。
- 可以通過 option.attributes.value && option.attributes.value.specified 來判斷 option 元素是否定義了 value 屬性。
故,獲得當前 select 元素值的腳本如下:
復制代碼 代碼如下:
var getSelectValue = funtion(select) {
var idx = select.selectedIndex,
option,
value;
if (idx > -1) {
option = select.options[idx];
value = option.attributes.value;
return (value && value.specified) ? option.value : option.text);
}
return null;
}
相關文章
js 獲取子節(jié)點函數(shù) (兼容FF與IE)
兼容FF與IE的獲取子節(jié)點的js代碼,需要的朋友可以參考下,我們剛不發(fā)布的dom操作文章。2010-04-04textbox 在光標位置插入字符功能的js實現(xiàn)(兼容ie,firefox)
document.selection.createRange是IE獨有的 而firefox也有獨有的一套2009-12-12checkbox 多選框 聯(lián)動實現(xiàn)代碼
對于checkbox 多選的聯(lián)動效果,是個不錯的思路,附代碼2008-10-10JavaScript動態(tài)調(diào)整TextArea高度的代碼
通過JavaScript根據(jù)TextArea的內(nèi)容動態(tài)調(diào)整TextArea的高度,需要的朋友可以參考下。2010-12-12