关注点:一、attr()和prop()的区别

<!DOCTYPE html>
<html>
<head>
<title>JavaScript对文字按照拼音排序</title>
<script src="jquery-1.11.2.min.js"></script>
<script>
function checkPut(){
var inputs=$(".radio");
var num=[];
inputs.each(function(){
//each() 是jquery里的循环
if($(this).prop('checked')){
debugger;
num.push($(this).val());
}
});
alert(num);
} function checkAll(){
debugger;
var inputs=$(".radio");
inputs.each(function(){
if(!$(this).prop('checked')){
$(this).prop('checked','checked');
}
});
}
function unCheckAll(){
var inputs=$('.radio');
inputs.each(function(){
if($(this).prop('checked')){
$(this).prop('checked',false);
}else{
$(this).prop('checked',true);
}
});
}
$(function(){
$("input[type=radio][value=1]").bind(
  "click",
  function(event){
event.preventDefault()
  }
);
$('input[type=radio][value=1]').mouseup(function(){
debugger;
if($('input[type=radio][value=1]').prop('checked')){
$('input[type=radio][value=1]').prop('checked',false);
}else{
$('input[type=radio][value=1]').prop('checked',true);
}
});
})
function aa(){
debugger;
console.log('aa');
console.log($('input[type=radio][value=1]'));
$('input[type=radio][value=1]').prop('checked',false);
}
</script>
</head>
<body>
<input type="radio" class="radio" value="1" /> 1
<input type="radio" class="radio" value="2" /> 2
<input type="radio" class="radio" value="3" /> 3
<input type="radio" class="radio" value="4" /> 4
<input type="radio" class="radio" value="5" /> 5
<input type="radio" class="radio" value="6" /> 6
<input type="submit" onclick="checkPut();"/>
<input type="button" value="全选" onclick="checkAll();"/>
<input type="button" value="反选" onclick="unCheckAll();"/> <input type="button" value="取消" onclick="aa();"/> </body>
</html>

  prop()函数针对的是DOM元素(JS Element对象)的属性,attr()函数针对的是DOM元素所对应的文档节点的属性。

(即:对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。)
例如:
<a href="http://www.baidu.com" target="_self" class="btn">百度</a>
 这个例子里<a>元素的DOM属性有“href、target和class",这些属性就是<a>元素本身就带有的属性,也是W3C标准里就包含有这几个属性,或者说在IDE里能够智能提示出的属性,这些就叫做固有属性。处理这些属性时,建议使用prop方法。 <a href="#" id="link1" action="delete">删除</a>
这个例子里<a>元素的DOM属性有“href、id和action”,很明显,前两个是固有属性,而后面一个“action”属性是我们自己自定义上去的,<a>元素本身是没有这个属性的。这种就是自定义的DOM属性。处理这些属性时,建议使用attr方法。使用prop方法取值和设置属性值时,都会返回undefined值。

<input id="chk1" type="checkbox" />是否可见
<input id="chk2" type="checkbox" checked="checked" />是否可见 像checkbox,radio和select这样的元素,选中属性对应“checked”和“selected”,这些也属于固有属性,因此需要使用prop方法去操作才能获得正确的结果。 $("#chk1").prop("checked") == false
$("#chk2").prop("checked") == true

如果使用attr,则:
$("#chk1").attr("checked") == undefined
$("#chk2").attr("checked") == "checked"
二、js事件绑定
$(function(){
$("input[type=radio][value=1]").bind(
  "click",
  function(event){
event.preventDefault()
  }
);
$('input[type=radio][value=1]').mouseup(function(){
debugger;
if($('input[type=radio][value=1]').prop('checked')){
$('input[type=radio][value=1]').prop('checked',false);
}else{
$('input[type=radio][value=1]').prop('checked',true);
}
});
})

禁用radio的click事件,添加mouseup事件,实现单选按钮反选。

 
												

关于radio选中或者反选的更多相关文章

  1. 使用 jQuery 实现 radio 的选中与反选

    使用 jQuery 实现 radio 的选中与反选 我们知道在 Html 中当我们选中一个radio后,再次点击该 radio,那么该 radio 依然是一个选中的状态,但是有时我们需要实现这样的逻辑 ...

  2. easyui 》 radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中

    获取一组radio被选中项的值var item = $('input[@name=items][@checked]').val();获取select被选中项的文本var item = $(" ...

  3. Jquery 操作 radio选中值

    1.获取radio选中值 1.1  $('input:radio:checked').val(); 1.2  $("input[type='radio']:checked").va ...

  4. 设置checkbox选中,设置radio选中,根据值设置checkbox选中,checkbox勾选

    设置checkbox选中,设置radio选中,根据值设置checkbox选中,checkbox勾选 >>>>>>>>>>>>&g ...

  5. jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中

    jQuery获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); //为Se ...

  6. springMvc接收ajax数组参数,以及jquery复选框选中、反选、全选、全不选

    一.复选框选中.反选.全选.全不选 html代码: <input type='checkbox' name='menuCheckBox' value='10' >苹果 <input ...

  7. query 中 radio选中小技巧

    在php中经常,经常要用到radio选中按钮,下次再登录时默认记录用户选中的选项,在PHP判断的时候: 在input中不能加checked=“<?php ;?>”:否则失效

  8. jQuery获取radio选中后的文字

    原文链接:http://blog.csdn.net/zhanyouwen/article/details/51393216 jQuery获取radio选中后的文字转载 2016年05月13日 10:3 ...

  9. JQuery控制radio选中和不选中方法总结

    一.设置选中方法 代码如下: $("input[name='名字']").get(0).checked=true; $("input[name='名字']"). ...

随机推荐

  1. 部署Jenkins完整记录

    Jenkins通过脚本任务触发,实现代码的自动化分发,是CI持续化集成环境中不可缺少的一个环节.下面对Jenkins环境的部署做一记录.-------------------------------- ...

  2. ANSI转义代码(ANSI escape code)

    ANSI escape code - Wikipedia linux 输出绿色的✓TRUE,红色的✗FALSE : echo -e "\x1B[1;32m✓TRUE \x1B[0mXXX&q ...

  3. 返回闭包不能引用循环变量,请改写count()函数,让它正确返回能计算1x1、2x2、3x3的函数。

    错误写法: 正确写法:

  4. 正则sub的使用

    import re # unicode 编码匹配范围[u4e00-u9fa5] pattern = re.compile('(\w+) (\w+)') s = 'hello 123,hello 456 ...

  5. MFC绘图基础

    ·MFC中三种坐标系统: 1.屏幕坐标系 坐标原点位于屏幕左上角 2.(非客户区)窗口坐标系 坐标原点位于窗口左上角(包括标题栏) 3.客户区坐标系 坐标原点位于客户区左上角(不包括标题栏) ·坐标系 ...

  6. VC6.0在Win8,10下的兼容性调整

    Microsoft Visual C++ 6.0,简称VC6.0,是微软推出的一款C++编译器,将“高级语言”翻译为“机器语言(低级语言)”的程 序.Visual C++是一个功能强大的可视化软件开发 ...

  7. Scrapy框架——安装以及新建scrapy文件

    一.安装 conda install Scrapy   :之后在按y 表示允许安装相关的依赖库(下载速度慢的话也可以借助镜像源),安装的前提是安装了anaconda作为python ,   测试scr ...

  8. 分布式服务防雪崩熔断器,Hystrix理论+实战。

    Hystrix是什么? hystrix对应的中文名字是"豪猪",豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netfl ...

  9. spark streaming 笔记

    spark streaming项目 学习笔记 为什么要flume+kafka? 生成数据有高峰与低峰,如果直接高峰数据过来flume+spark/storm,实时处理容易处理不过来,扛不住压力.而选用 ...

  10. Rikka with Nickname (简单题)

    Rikka with Nickname  链接:https://www.nowcoder.com/acm/contest/148/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒空间限制:C/ ...