Js获取下拉框当前选择项的文本和值
现在有一个Id为AreaId的下拉框,要获取它当前选择项的文本和值有以下方法:
<span class="red">* </span>
地 区:
<span>
<select id="AreaId" name="AreaId" size="1" class="sel">
<option>-请选择地区-</option>
<option value="1">北京</option>
<option value="2">上海</option>
<option value="3">深圳</option>
</select>
</span>
方法一:使用JavaScript原生态的方法.
1.获取值:
document.getElementById("AreaId").value;//有效,能得到正确的值.
var index=document.getElementById("AreaId").selectedIndex;//获取当前选择项的索引.
document.getElementById("AreaId").options[index].value;//获取当前选择项的值.
var obj=document.getElementById("AreaId");
for(i=0;i<obj.length;i++) {//下拉框的长度就是它的选项数.
if(obj[i].selected==true) {
var text=obj[i].value;//获取当前选择项的值.
}
}
2.获取文本:
var index=document.getElementById("AreaId").selectedIndex;//获取当前选择项的索引.
document.getElementById("AreaId").options[index].text;//获取当前选择项的文本.
document.getElementById("AreaId").options[index].innerHTML;//获取当前选择项的文本.
var obj=document.getElementById("AreaId");
for(i=0;i<obj.length;i++) {//下拉框的长度就是它的选项数.
if(obj[i].selected==true) {
var text=obj[i].text;//获取当前选择项的文本.
}
}
document.getElementById("AreaId").text;//注意,这句代码无效,得到的结果为undefined.
方法二:使用JQuery方法(前提是已经加载了jquery库).
1.获取值:
$("#AreaId").val();//获取当前选择项的值.
var options=$("#AreaId option:selected");//获取当前选择项.
options.val();//获取当前选择项的值.
2.获取文本:
var options=$("#AreaId option:selected");//获取当前选择项.
options.text();//获取当前选择项的文本.
options.innerHTML();//获取当前选择项的文本.
$("#AreaId").text;//注意,这句代码无效,得到的结果为undefined.
其他属性:
innerText:
var index=document.getElementById("AreaId").selectedIndex;//获取当前选择项的索引.
document.getElementById("AreaId").options[index].innerText;//获取当前选择项的文本,IE支持,Firefox不支持.
document.getElementById("AreaId").innerHTML;//获取当前下拉框所有的元素,包括Html代码.注意大小写.
document.getElementById("AreaId").textContent;//获取当前下拉框中所有的选择项文本,不包括Html代码.
感谢您怀着耐心看完整篇博文!!!
如果文章有什么错误或不当之处,请您斧正!
您有任何意见或者建议,您可以给我发邮件,也可以在下面留言,我看到了会第一时间回复您的,谢谢!
Js获取下拉框当前选择项的文本和值的更多相关文章
- Js获取下拉框选定项的值和文本
Js获取下拉框的值和文本网上提供了2种方法:但有些人很不负责任,他们根本没考虑到浏览器之间的差异导致的错误,导致很多新手琢磨了半天找不出错误! 下面我总结下Firefox和IE下获取下拉框选定项的值和 ...
- 原生js获取下拉框下标
// 获取下拉框所选下标 传入下拉框的id function getselectscheckitemindex (idStr) { let o = document.getElementById(id ...
- js总结:利用js获取下拉框的value值和文本值
select下拉框在项目开发中是经常用到的,特别是在联级菜单方面的应用更为广泛.但是,对于一些初学者来说,如何获取下拉框子节点option的value值和文本内容,还是有一点难度的. html代码: ...
- js获取下拉框的值
获取select 选中的option的值: $("#ddlRegType").find("option:selected").val(); 获取select ...
- Js获取下拉框的值和文本select
$("#camera").change(function () { var obj = this; $("#camera_Name" ...
- js获取下拉框当前选中的值并弹出
this.options[this.selectedIndex].value --- 显示文本 this.value --- 实际存储值 调用实例: <script language=" ...
- js 获取 下拉框的值
//错误 console.log($("#DictID").select.val()); //错误 console.log($("#DictID").selec ...
- js获取下拉框的value值
var Resultstr=""; var param = { action: "MoneyList" };//参数拼接 $.ajax({ type: &quo ...
- js,jquery获取下拉框选中的option
js获取select选中的值: var sel=document.getElementById("select1"); var index = sel.selectedIndex; ...
随机推荐
- RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组件WinForm业务平台
RDIFramework.NET ━ .NET快速信息化系统开发框架 工作流程组件WinForm业务平台 接上篇: RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组 ...
- [Android Tips] 17. Check APK Sign Info
$ jarsigner -verify -verbose -certs <apk file path> 查看 keystore $ keytool -list -keystore debu ...
- pepperflash
sudo apt-get install pepperflashplugin-nonfree sudo update-pepperflashplugin-nonfree --install
- 学习OpenCV——SVM 手写数字检测
转自http://blog.csdn.net/firefight/article/details/6452188 是MNIST手写数字图片库:http://code.google.com/p/supp ...
- 同事的游戏项目--Robocode-学习链接
Robocode机器人库学习链接:http://www.pudn.com/search_db.asp?keyword=Robocode 官网 :http://robocode.sourceforge. ...
- 借助Glances Monitor,密切关注你的系统
两种方法安装 glances 通常可以有两种方法安装 glances.第一种是通过编译源代码的方式,这种方法比较复杂另外可能会遇到软件包依赖性问题.还有一种是使用特定的软件包管理工具来安装 glanc ...
- RuntimeWarning: invalid value encountered in divide
import numpy as np olderr = np.seterr(all='ignore') 在程序的开头加上如上代码 https://docs.scipy.org/doc/numpy/re ...
- 单用户模式下连接被占用定位spid
报错The database is in single-user mode, and a user is currently connected to it. 通过 select * from sys ...
- SQL2005中的事务与锁定(五)- 转载
------------------------------------------------------------------------ -- Author : HappyFlyStone - ...
- 最快速的Android开发环境搭建ADT-Bundle及Hello World
ADT-Bundle for Windows 是由Google Android官方提供的集成式IDE,已经包含了Eclipse,你无需再去下载Eclipse,并且里面已集成了插件,它解决了大部分新手通 ...