js 獲取當(dāng)前select元素值的代碼
更新時(shí)間:2010年04月12日 13:49:52 作者:
對(duì)于value值可以通過(guò)select.value 取得的,但對(duì)于text值則需要通過(guò)select.options[select.sekectedIndex].txet 獲得。
1、如果 select 元素下的所有 option 元素均沒(méi)有指定 selected 屬性,會(huì)默認(rèn)選中第一個(gè)。
2、可以通過(guò) select.selectedIndex 獲取到選中的 option 元素的索引。
3、可以通過(guò) select.options[select.selectedIndex] 獲取到選中的 option 元素。
4、option 元素 <option selected="selected" value="value3">text3</option>,可以通過(guò) option.value 獲得 option 元素的 value 屬性值,即 value3;可以通過(guò) option.text 獲得 option 元素內(nèi)的文本,即 text3。
5、如果 option 元素沒(méi)有定義 value 屬性,則 IE 中 option.value 無(wú)法獲得,但 Safari、Opera、FireFox 依舊可以通過(guò) option.value 獲得,值同于 option.text 。
6、可以通過(guò) option.attributes.value && option.attributes.value.specified 來(lái)判斷 option 元素是否定義了 value 屬性。
故,獲得當(dāng)前 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;
}
以前由于兼容性問(wèn)題,大家用select.options[select.sekectedIndex].value來(lái)取值,不過(guò)現(xiàn)在用select.value都可以了
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
事件觸發(fā)也可以用
<select id="jb51net" onchange="alert(getSelectValue(this))">
最好是綁定事件。
2、可以通過(guò) select.selectedIndex 獲取到選中的 option 元素的索引。
3、可以通過(guò) select.options[select.selectedIndex] 獲取到選中的 option 元素。
4、option 元素 <option selected="selected" value="value3">text3</option>,可以通過(guò) option.value 獲得 option 元素的 value 屬性值,即 value3;可以通過(guò) option.text 獲得 option 元素內(nèi)的文本,即 text3。
5、如果 option 元素沒(méi)有定義 value 屬性,則 IE 中 option.value 無(wú)法獲得,但 Safari、Opera、FireFox 依舊可以通過(guò) option.value 獲得,值同于 option.text 。
6、可以通過(guò) option.attributes.value && option.attributes.value.specified 來(lái)判斷 option 元素是否定義了 value 屬性。
故,獲得當(dāng)前 select 元素值的腳本如下:
復(fù)制代碼 代碼如下:
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;
}
以前由于兼容性問(wèn)題,大家用select.options[select.sekectedIndex].value來(lái)取值,不過(guò)現(xiàn)在用select.value都可以了
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
事件觸發(fā)也可以用
<select id="jb51net" onchange="alert(getSelectValue(this))">
最好是綁定事件。
您可能感興趣的文章:
- 使用原生JS獲取select元素選中的value和text值
- javascript獲取select值的方法完整實(shí)例
- JavaScript實(shí)現(xiàn)獲取select下拉框中第一個(gè)值的方法
- JavaScript獲取select中text值的方法
- javascript獲取select標(biāo)簽選中的值
- javascript獲取select值的方法分析
- js獲取select標(biāo)簽選中值的兩種方式
- jquery及原生js獲取select下拉框選中的值示例
- js獲取當(dāng)前select 元素值的代碼
- JavaScript獲取select選中值的兩種方法實(shí)現(xiàn)
相關(guān)文章
鼠標(biāo)移動(dòng)到某個(gè)單元格上后,整個(gè)列都變色的實(shí)現(xiàn)方法
鼠標(biāo)移動(dòng)到某個(gè)單元格上后,整個(gè)列都變色的實(shí)現(xiàn)方法...2007-01-01js textarea自動(dòng)增高并隱藏滾動(dòng)條
textarea自動(dòng)增高并隱藏滾動(dòng)條2009-12-12強(qiáng)效、方便的表單通用檢測(cè)JS 不錯(cuò)
強(qiáng)效、方便的表單通用檢測(cè)JS 不錯(cuò)...2007-01-01javascript select控件間內(nèi)容互相移動(dòng)
javascript select中內(nèi)容互相移動(dòng)2009-11-11仿校內(nèi)登陸框,精美,給那些很厲害但是沒(méi)有設(shè)計(jì)天才的程序員
寫(xiě)代碼對(duì)于我們來(lái)說(shuō)很簡(jiǎn)單,但是做個(gè)布局設(shè)計(jì)簡(jiǎn)直是登天啊2008-11-11