HTML中的select只读
因为Select下拉框只支持disabled属性,不支持readOnly属性,而在提交时,disabled的控件,又是不提交值的。现提供以下几种解决方案:
1、在html中使用以下代码,在select外层加1个span,通过js控制。这种设置的不足之处是IE浏览器兼容,fireFox及其他不兼容.. <span onmousemove="this.setCapture();"onmouseout="this.releaseCapture();" onfocus="this.blur();">
<select id="select1">
<option value="0">0</option>
<option value="1">1</option>
</select>
</span>
2、使用js文件,这种方法的不足之处和第一种一样。 <select name="select">
<option>aaa</option>
</select>
<script type="text/javascript">
SetReadOnly(document.getElementById("select"));
function SetReadOnly(obj){
if(obj){
obj.onbeforeactivate = function(){return false;};
obj.onfocus = function(){obj.blur();};
obj.onmouseover = function(){obj.setCapture();};
obj.onmouseout = function(){obj.releaseCapture();};
}
}
</script>
3、使用jquery方式解决。 $(function(){ $("select").attr("disabled", "disabled");
//如果和jquery1.6以上版本,可以使用以下语句:
$("select").prop("disabled", true);});
4、先将select的属性设置为
disabled="disabled"
然后在提交表单的时候使用disabled置为空。
Microsoft IE 5.5、IE 6、IE7、IE 10、Mozilla Firefox、Apple Safari 和 Opera 等浏览器对 HTML select 元素的 disabled 属性支持都很不错。而 IE 8、IE 9 和 Chrome 都有 bug,不能很好支持。不知道有没有办法在 HTML 源代码补救这一 bug。
补救办法: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link href="Main.css" type="text/css" rel="stylesheet" />
<title>Test</title>
</head>
<body>
<div>
<select size="5" disabled="disabled">
<option value="C1">Black</option>
<option value="C2">DarkCyan</option>
<option value="C3" selected="selected" class="selected">DarkRed</option>
<option value="C4">DarkMagenta</option>
</select>
<select size="5">
<option value="C1">Black</option>
<option value="C2">DarkCyan</option>
<option value="C3" selected="selected">DarkRed</option>
<option value="C4">DarkMagenta</option>
</select>
<input type="text" />
</div>
</body>
</html>
其中 Main.css 如下所示: option.selected {
color: White;
background-color: Cyan;
}
其他改变样式,使用CSS改变文字颜色 用 CSS 定义文字颜色如下代码所示: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css"> select { color:red } </style>
<title>Test</title>
</head>
<body>
<div>
<select size="5" disabled="disabled">
<option value="C1">Black</option>
<option value="C2">DarkCyan</option>
<option value="C3" selected="selected">DarkRed</option>
<option value="C4">DarkMagenta</option>
</select>
<select size="5">
<option value="C1">Black</option>
<option value="C2">DarkCyan</option>
<option value="C3" selected="selected">DarkRed</option>
<option value="C4">DarkMagenta</option>
</select>
<input type="text" />
</div>
</body>
</html>
5、使用隐藏域,在select下面设置隐藏域,显示的时候disabled,提交的时候提交隐藏域中的值。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<select id="selList" onchange="setVal()">
<option value="1" >1</option>
<option value="2" selected="selected">2</option>
</select>
<input id="hdSelList" type="text" />
<script type="text/javascript">
//本demo是为了清晰地表达, 你在select中加入 disabled="disabled",
//将input中的type改为hidden即为你要的效果了.
//提交时, 你不要取selList的值, 取hdSelList的值就好了.
setVal(); //1.在初始加载时就将两者的值设置为一致;
//2. 为了防止代码以后会有改动---有时不需要disabled, 故有上面的onchange="setVal()"
function setVal() {
document.getElementById('hdSelList').value = document.getElementById('selList').value;
}
</script>
</body>
</html>
还有下面的一种情况,页面数据太多,处理时间较长 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="../ec/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript" >
function commit() {
$DisSelects = $("select[disabled='disabled']");//获取所有被禁用的select
$DisSelects.attr("disabled", false); //处理之前, 全部打开
$("#form1").submit(); //提交
$DisSelects.attr("disabled", true); //处理完成, 全部禁用
}
</script>
</head>
<body>
<form id="form1" action="HTMLPage.htm">
<input type="button" value="submit" onclick="commit()" />
<select id="Select1" disabled="disabled" >
<option value="0" >0</option>
<option value="1" selected="selected">1</option>
</select>
<select id="Select2" disabled="disabled" >
<option value="1" >1</option>
<option value="2" selected="selected">2</option>
</select>
<select id="Select3" disabled="disabled" >
<option value="2" >2</option>
<option value="3" selected="selected">3</option>
</select>
<select id="Select4" disabled="disabled" >
<option value="3" >3</option>
<option value="4" selected="selected">4</option>
</select>
</form>
</body>
</html>
今天在网上看到select的只读问题,第一反应是readonly,后来发现select 没有readonly,因此去网上查了一下。发现有以下解决方案:
HTML中的select只读的更多相关文章
- 设置SQLServer数据库中某些表为只读的多种方法
原文:设置SQLServer数据库中某些表为只读的多种方法 翻译自:http://www.mssqltips.com/sqlservertip/2711/different-ways-to-make- ...
- mysql: update字段中带select
update字段中带select UPDATE tb_report_type A INNER JOIN (SELECT LEVEL_CODE FROM tb_report_type WHERE id ...
- HTML中的<select>标签如何设置默认选中的选项
方法有两种. 第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果. 1 2 3 4 5 < select id = " ...
- Access中的SELECT @@IDENTITY
在Access数据库中存在select @@identity吗?答案是肯定的.但是Access一次只能执行一条SQL,多条SQL需要多次执行,这是限制.在SQL Server中,可以一次执行多条SQL ...
- 2.3 LINQ查询表达式中 使用select子句 指定目标数据
本篇讲解LINQ查询的三种形式: 查询对象 自定义查询对象某个属性 查询匿名类型结果 [1.查询结果返回集合元素] 在LINQ查询中,select子句和from子句都是必备子句.LINQ查询表达式必须 ...
- python中的select模块
介绍: Python中的select模块专注于I/O多路复用,提供了select poll epoll三个方法(其中后两个在Linux中可用,windows仅支持select),另外也提供了kqu ...
- 存储过程中使用select……into
在MySQL存储过程中使用SELECT -INTO语句为变量赋值: 用来将查询返回的一行的各个列值保存到局部变量中. 要求: 查询的结果集中只能有1行. SELECT col_name[,...] I ...
- I/O复用中的 select poll 和 epoll
I/O复用中的 select poll 和 epoll: 这里有一些不错的资料: I/O多路复用技术之select模型: http://blog.csdn.net/nk_test/article/de ...
- MySQL在字段中使用select子查询
前几天看别人的代码中看到在字段中使用select子查询的方法,第一次见这种写法,然后研究了一下,记录下来 大概的形式是这样的: select a .*,(select b.another_field ...
随机推荐
- an interview question(1)
声明:本文为博主原创文章,未经博主允许不得转载. 以下是英文翻译: warnning: Copyright!you can't reprint this blog when you not get b ...
- 理论基础知识之————KB Kb Kbps 相关单位的区别和换算
换算公式 8bit(位)=1Byte(字节) 1024Byte(字节)=1KB 1024KB=1MB 1024MB=1GB 1024GB=1TB 容量是大写的 B 而传输的速度是小写的 b bps ...
- DataTable转换为Json字符串的三种方法
//第一种:使用StringBuilder public string DataTableToJson(DataTable table) { var JsonString = new StringB ...
- Rigidbody相关的操作最好放在FixedUpdate中,update中可能会无效果
void Turning() { // Create a ray from the mouse cursor on screen in the direction of the camera. Ray ...
- aaa
aaaa 来自为知笔记(Wiz)
- Python学习笔记之字典
一.创建和使用字典 1.创建字典 phonebook={'Alice':'2341','Beth':'9102','Cecil':'3258'} 2.dict,通过映射创建字典 >>> ...
- <input type="file" id="camera" multiple="multiple" capture="camera" accept="image/*"> 上传图片,手机调用相册和摄像头
<input type="file" id="camera" multiple="multiple" capture="ca ...
- MySQL update时使用联表,聚合
原文地址 http://stackoverflow.com/questions/3022405/mysql-update-query-with-left-join-and-group-by UPDAT ...
- java selenium针对多种情况的多窗口切换
一.通过按钮点击打开的新页面,不涉及到打开多窗口,只要在已有打开的窗口实现切换操作即可 Set<String> winHandels = driver.getWindowHandles() ...
- nullable,nonnull, null_resettable以及_Null_unspecified的区别和使用
1.关键字:可以用于属性 方法和返回值参数中 关键字作用:提示作用 告诉开发者属性信息 关键字的目的:迎合swift 强语言,swift必须要指定一个对象是否为空 关键字好处:提高代码规划,减少沟通 ...