jquery选中radio或checkbox的正确姿势
jquery选中radio或checkbox的正确姿势
Intro
前几天突然遇到一个问题,没有任何征兆的。。,jquery 选中radio button单选框时,一直没有办法选中,后来查了许多资料,发现自己的姿势有问题。
Issue
我按下面的方式选中 radio 时,发现有时候会选不中。
<label class="t-radio t-circle-radio t-green-radio"><input type="radio" value="1" onchange="$('#saleInfo').show()" checked="checked" name="isOnSale" />加入</label>
<label class="t-radio t-circle-radio t-green-radio"><input type="radio" value="0" onchange="$('#saleInfo').hide()" name="isOnSale" />不加入</label>
下面是我的 js 代码
//前面已引用 jquery.js 后文不再赘述
...
$("input[type='radio'][name='isOnSale'][value='1']").attr("checked","checked");
Solution0
区分 attribute 和 property
attribute 和 property 是不同的
property 是 html 标签固有的属性,而 attribute 多是 html 自定义属性。
attribute是html文档上标签属性,而 property 则是对应 DOM 元素的自身属性。 从操作方法上来看,attribute可以通过 DOM规范的 getAttribute
和 setAttribute
进行获取修改,而property可以通过对象访问属性的方式 .
或者 [" "]来修改获取。 jquery 获取或设置 attribute 用的是 attr
,获取或设置 property 用的是 prop
Property
DOM 节点是一个对象,所以它像 JavaScript 的对象一样可以存储自定义的属性和方法。
Attribute
DOM节点可以通过以下标准方法访问 HTML 的 attribute
elem.hasAttribute(name) - checks if the attribute exists
elem.getAttribute(name) - gets an attribute value
elem.setAttribute(name, value) - sets an attribute
elem.removeAttribute(name) - removes an attribute
checked 是 input 标签的属性(property)而不是 attribute ,参见 http://www.w3school.com.cn/tags/att_input_checked.asp
更多 input
相关的属性详见: http://www.w3school.com.cn/tags/tag_input.asp
Solution1
HACK:mock click
设置选中之后调用对象的click()方法,模拟点击
//toogle
$("input:radio[name='isOnSale'][value='1']").click();
Solution2
设置 input 的 checked 属性
原生js
//check
document.getElementsByName("isOnSale")[0].checked = true;
//uncheck
document.getElementsByName("isOnSale")[0].checked = false;
jquery
//
$("input[type='radio'][name='isOnSale'][value='1']").prop("checked",true);
More
如果你有别的更好的解决方案,欢迎指出。
如果有什么问题,欢迎联系我 ben121011@126.com
jquery选中radio或checkbox的正确姿势的更多相关文章
- jquery的radio和checkbox的标签的操作集合
jquery的radio和checkbox的标签的操作集合: $("input[name='radio_name'][checked]").val(); //选择被选中Radio的 ...
- JQuery触发radio或checkbox的change事件
在JQuery中,当给radio或checkbox添加一个change事件时,如果它的值发生变化就会触发change事件;本文将详细介绍如何利用JQuery触发Checkbox的change事件需要了 ...
- jquery实用应用之jquery操作radio、checkbox、select
本文收集一些jquery的实用技巧,非常实用的哦,其中对radio.checkbox.select选中与取值的方法. 获取一组radio被选中项的值var item = $('input[@name= ...
- jquery对radio和checkbox的操作
jQuery获取Radio选择的Value值 代码 $("input[name='radio_name'][checked]").val(); //选择被选中Radio的Valu ...
- 《jQuery判断radio、checkbox、select 是否选中和设置选中问题以及获取选中值》总结
<form> <input type="radio" name="gender" id="man" value=" ...
- [转]jQuery操作radio、checkbox、select 集合.
1.radio:单选框 html代码 <input type="radio" name="radio" id="radio1" val ...
- jQuery操作radio、checkbox、select 集合
1.radio:单选框 HTML代码: <input type="radio" name="radio" id="radio1" va ...
- jQuery操作radio、checkbox、select总结
1.radio:单选框 HTML代码: <input type="radio" name="radio" id="radio1" va ...
- JQuery 选中Radio
<tr> <td> <input type="radio" name="rdb" value="启用" che ...
随机推荐
- java 读取文件的路径
1. 通用定位到用户目录下: String userDir = System.getProperty("user.dir"); 2. web项目定位到WEB-INF/class ...
- 【Django】Django与jinja的不同
1. http://jinja.pocoo.org/docs/dev/switching/#django 目前已了解的不同: 1) 某元素是否存在 if xxx Django if xxx is ...
- node.js fs.open 和 fs.write 读取文件和改写文件
Node.js的文件系统的Api //公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile( ...
- JStorm与Storm源码分析(五)--SpoutOutputCollector与代理模式
本文主要是解析SpoutOutputCollector源码,顺便分析该类中所涉及的设计模式–代理模式. 首先介绍一下Spout输出收集器接口–ISpoutOutputCollector,该接口主要声明 ...
- [BZOJ 2500] 幸福的道路
照例先贴题面(汪汪汪) 2500: 幸福的道路 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 368 Solved: 145[Submit][Sta ...
- ABP从入门到精通(3):aspnet-zero-core 使用Redis缓存
一.Redis是什么? redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset ...
- Android中的Activity
Android四大组件 活动(Activity) 广播接收者(BroadCastReceiver) 服务(Service) 内容提供者(Contentprovider) Activity 先来看And ...
- CentOS6.3 下安装codeblocks
本人用的系统是centos6.3(虚拟机) 需要预先安装gcc编译器(参考:http://www.cnblogs.com/magialmoon/archive/2013/05/05/3061108.h ...
- vue+mockjs 模拟数据,实现前后端分离开发
在项目中尝试了mockjs,mock数据,实现前后端分离开发. 关于mockjs,官网描述的是 1.前后端分离 2.不需要修改既有代码,就可以拦截 Ajax 请求,返回模拟的响应数据. 3.数据类型丰 ...
- java正则表达式提取地址中的ip和端口号
由于我需要用到java正则表达式提取地址中的ip和端口号,所以我就写了一个demo,测试一下,下面是demo public class Test0810_1 { public static void ...