首先说一下readonly属性的应用场景

  1. 表单中,不能编辑对应的文本,但是仍然可以聚焦焦点
  2. 在提交表单的时候,该输入项会作为form的一项提交(目的)

这里要说一下disabled和readonly的不同,如果一个输入项的disabled设为true,则该表单输入项不能获取焦点,用户的所有操作(鼠标点击和键盘输入等)对该输入项都无效,最重要的一点是当提交表单时,这个表单输入项将不会被提交。

input标签实现readonly效果

input标签需要readonly效果的,通常是type=text/checkbox/radio,下面一一介绍这三种类型的readonly效果实现。

<!-- input标签type=text的readonly效果实现 -->
<input type="text" readonly="readonly" value="readonly" />
<!-- input标签type=checkbox的readonly效果实现 -->
<input type="checkbox" name="checkbox1" value="checkbox1" id="red" checked="checked" />
<label for="red">红色</label>
<input type="checkbox" name="checkbox2" value="checkbox2" id="color" />
<label for="color">颜色</label>
<script>
//JS实现readonly效果
$('input[type="checkbox"]').bind("click", function(){
this.checked = !this.checked;
});
</script>
<!-- input标签type=radio的readonly效果实现 -->
<input type="radio" name="radio" value="radio1" id="red" checked="checked" />
<label for="red">红色</label>
<input type="radio" name="radio" value="radio2" id="blue" />
<label for="blue">蓝色</label>
<script>
//JS实现readonly效果
$('input[type="radio"]').each(function(){
if($(this).attr("checked") != "checked"){
$(this).attr("disabled", true);
}
});
</script>

textarea标签实现readonly效果

<!-- textarea标签的readonly效果实现 -->
<textarea readonly="readonly">不可编辑</textarea>

select标签实现readonly效果

<!-- select标签readonly效果实现 -->
<select name="readonly">
<option value="red" selected="selected">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
</select>
<script>
//JS实现readonly效果
$('select').focus(function(){
this.defaultIndex = this.selectedIndex;
}); $('select').change(function(){
this.selectedIndex = this.defaultIndex;
});
</script>

如果值是固定的话,传输参数也可以通过设置隐藏域实现,让原本显示的disabled为true就行,如:

<!-- 设置隐藏域,传输数据 -->
<input type="hidden" name="hide" value="hide" />
<input type="text" name="hide" value="hide" disabled="disabled">

readonly的展示效果没disabled好,disabled让用户知道这是不可编辑的,而readonly会给用户一种错觉。

input/select/textarea标签的readonly效果实现的更多相关文章

  1. 设置onselectstart在ie浏览器下对于input及textarea标签页会生效

    设置onselectstart在ie浏览器下对于input及textarea标签页会生效, 可以使用js来控制对于某种标签不生效,代码如下: document.onselectstart = disa ...

  2. input和textarea标签的select()方法----选中文本框中的所有文本

    JavaScript select()方法选中文本框中的所有文本 <input>和<textarea>两种文本框都支持select()方法,这个方法用于选择文本框中的所有文本 ...

  3. jquery的clone方法bug的修复select,textarea的值丢失

    项目中多次使用了iframe,但是操作起来是比较麻烦,项目中的现实情况是最外面是一个form,里面嵌套一个iframe,下面是一个其他的数据,在form提交的时候将iframe的数据和其他的数据一块提 ...

  4. iScroll滚动区域中select、input、textarea元素无法点击的Bug修复

    最近在一个项目中使用了iScroll4模拟滚动效果,调试过程中发现一个表单页中的所有表单项都无法点击聚焦, 如<select>.<input>.<textarea> ...

  5. input/radio/select等标签的值获取和赋值

    input/radio/select等标签的值获取和赋值,这几个是使用率最高的几个标签,获取值和赋值以及初始化自动填充数据和选择: 页面html: <div class=" " ...

  6. JavaScript为input/textarea自定义hover,focus效果

    <title>JavaScript为input/textarea自定义hover,focus效果</title> <script type="text/java ...

  7. 第7天:input和label标签

    今天学的不多,就只学了表单元素中的input和label标签.搬了房子,收拾了一下东西,太累了,所以没有学很多.明天还上班,今天就先到这. 一.input input标签type属性有以下几个:tex ...

  8. [转]SpringMVC<from:form>表单标签和<input>表单标签简介

    原文地址:https://blog.csdn.net/hp_yangpeng/article/details/51906654 在使用SpringMVC的时候我们可以使用Spring封装的一系列表单标 ...

  9. python 之 前端开发(form标签、单选框、多选框、file上传文件、按钮、label标签、下拉列表、textarea标签、fieldset标签、table标签)

    11.25 form标签 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

随机推荐

  1. Linux之线程管理

    linux下查看线程数的几种方法   1. cat /proc/${pid}/status [root@limt01 2325]# ps -ef|grep xinetd|grep -v grep ro ...

  2. bug: 在使用HMSegmentedControl时,设置selectionIndicatorEdgeInsets对左右边界没有用

    若设置了    self.tabSegmented.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe; 则必须使用sel ...

  3. Red KV数据 庫设計模式

    转:http://blog.nosqlfan.com/html/3033.html NoSQL带给我们的东西很多,高性能,水平扩展性,还有不一样的思维方式.本文来自@hoterran的个人博客运维与开 ...

  4. StoryBoard--看上去很美

    StoryBoard--看上去很美 介绍 StoryBoard 是苹果在 2011 年的 WWDC Session 309<Introducing Interface Builder Story ...

  5. android中的ActionBar和ToolBar

    一.ToolBar 1.概述 Google在2015的IO大会上发布了系列的Material Design风格的控件.其中ToolBar是替代ActionBar的控件.由于ActionBar在各个安卓 ...

  6. [LintCode] Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Have you met ...

  7. 给ListView视图添加行号

    最后的效果如下: 更新,集成一个独立的模块而不改变源文件的方式更为稳妥. 购买地址:https://item.taobao.com/item.htm?spm=a1z10.3-c.w4002-26531 ...

  8. linux安装memcached及memcache扩展

    一.安装libevent函数库 下载地址:http://libevent.org 默认被安装到:/usr/local/lib目录下 安装memcached之前需要先安装libevent函数库. 可以通 ...

  9. SQL server while语句、存储过程

    1.循环语句 2.存储过程 存储过程(stored procedure)有时也称为sproc.存储过程存储于数据库中而不是在单独的文件中,有输入参数.输出参数以及返回值等. 3.四种存储过程类型(类似 ...

  10. spring security动态管理资源结合自定义登录页面

    如果想将动态管理资源与自定义登录页面一起使用,最简单的办法就是在数据库中将登录页面对应的权限设置为IS_AUTHENTICATED_ANONYMOUSLY. 因此在数据库中添加一条资源信息. INSE ...