Js获取下拉框选定项的值和文本
Js获取下拉框的值和文本网上提供了2种方法:但有些人很不负责任,他们根本没考虑到浏览器之间的差异导致的错误,导致很多新手琢磨了半天找不出错误!
下面我总结下Firefox和IE下获取下拉框选定项的值和文本:
1. IE和Firefox都支持的方法:
获取文本
var obj=document.getElementById('select_template');
var text=obj.options[obj.selectedIndex].text;//获取文本
var obj=document.getElementById("select_template");
for(i=0;i<obj.length;i++) {//下拉框的长度就是他的选项数
if(obj[i].selected==true) {
var text=obj[i].text;//获取文本
}
}
二者相比上一个方法比较简洁
2. IE支持Firefox不支持:
var obj=document.getElementById(name);
for(i=0;i<obj.length;i++) {
if(obj[i].selected==true) {
var text= obj[i].innerText;
}
}
获取值方法IE和Firefox通用:
var value=document.getElementById("select_template").value;//获取值
总结:其实主要就是IE和Firefox都支持value和text属性,Firefox不支持innerText属性。
Js实现当前页打开一个新链接:
window.location.href=url;
Js获取下拉框选定项的值和文本的更多相关文章
- js获取下拉框当前选中的值并弹出
this.options[this.selectedIndex].value --- 显示文本 this.value --- 实际存储值 调用实例: <script language=" ...
- 原生js获取下拉框下标
// 获取下拉框所选下标 传入下拉框的id function getselectscheckitemindex (idStr) { let o = document.getElementById(id ...
- Js获取下拉框当前选择项的文本和值
现在有一个Id为AreaId的下拉框,要获取它当前选择项的文本和值有以下方法: <span class="red">* </span> 地 区: ...
- js总结:利用js获取下拉框的value值和文本值
select下拉框在项目开发中是经常用到的,特别是在联级菜单方面的应用更为广泛.但是,对于一些初学者来说,如何获取下拉框子节点option的value值和文本内容,还是有一点难度的. html代码: ...
- js获取下拉框的值
获取select 选中的option的值: $("#ddlRegType").find("option:selected").val(); 获取select ...
- JS 动态显示 获取下拉框的多个值
<script type="text/javascript"> function GetProcessVal(i, t) { document.getElementsB ...
- Js获取下拉框的值和文本select
$("#camera").change(function () { var obj = this; $("#camera_Name" ...
- js 获取 下拉框的值
//错误 console.log($("#DictID").select.val()); //错误 console.log($("#DictID").selec ...
- js获取下拉框的value值
var Resultstr=""; var param = { action: "MoneyList" };//参数拼接 $.ajax({ type: &quo ...
随机推荐
- Yii源码阅读笔记(二十三)
Module类中的辅助功能方法: /** * Returns an ID that uniquely identifies this module among all modules within t ...
- 使用 U盘 重装 Mac OSX
一.制作 U 盘系统启动盘 1.从 App Store 上下载 OS Application.(这里需要注意,取消下载完的自动更新,并存储下这个 OS.Application 文件,因为系统更新完后, ...
- Hibernate n+1问题
转自: http://www.blogjava.net/RoyPayne/archive/2012/01/30/369017.htmlhttp://msi110.iteye.com/blog/7101 ...
- iOS:Xcode8以下真机测试iOS10.0和iOS10.1配置包
一.介绍 xcode的升级都已经到8系列了,可是还是有很多开发者使用的xcode还是7系列,然而xcode7...最多支持9.3,无法给升级到10.0和10.1的iPhone手机用户进行真机测试.此时 ...
- UIWebView通过JS语句获取网页(html)的某些数值
//To get string from the title of the HTML page: NSString *currentURL = [theWebView stringByEvaluati ...
- 查看ADOP会话
查看ADOP有哪些会话: $ adop -status Enter the APPS username: apps Enter the APPS password: Current Patching ...
- PRML读书笔记——Mathematical notation
x, a vector, and all vectors are assumed to be column vectors. M, denote matrices. xT, a row vcetor, ...
- ubuntu之使用sublime text3搭建Python IDE
参考文章: 教你如何将 Sublime 3 打造成 Python/Django IDE开发利器 Ubuntu16.04下使用sublime text3搭建Python IDE 如何优雅地使用Subli ...
- Leetcode: Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- java-mvc
定义 一种开发模式 Model-View-Controller Model 模型层 实体类.DAO(模型层实现数据库访问和业务逻辑) Controller 控制层 Servler.Filter(控制层 ...