关注点:一、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. Python中的浮点数原理与运算分析

    Python中的浮点数原理与运算分析 本文实例讲述了Python中的浮点数原理与运算.分享给大家供大家参考,具体如下: 先看一个违反直觉的例子:     >>> s = 0. > ...

  2. Websphere如何查看后台的日志以及简单应用

    文章目录 查找日志 简单应用: 安装应用 查找日志 /opt/IBM/WebSphere/AppServer/profiles/default/logs/server1/SystemOut.log 这 ...

  3. JS高级程序随笔二

    var person1={ toLoginString:function(){ return "lili"; }, toString2:function(){ return &qu ...

  4. 帮助_NOI导刊2010提高(03)

    题目描述 Bubu的书架乱成一团了!帮他一下吧! 他的书架上一共有n本书.我们定义混乱值是连续相同高度书本的段数.例如,如果书的高度是30,30,31,31,32,那么混乱值为3,30,32,32,3 ...

  5. SpringBoot-技术专区-配置文件加密

    工程中的配置文件如果把数据库的用户名密码写成明文的话是一件很危险的事情,之前也看见网上说可以对密码进行加密,用的时候再解密,因此今天我就尝试如何在spring boot 中的项目中实现关键信息的加密解 ...

  6. 53.Coin Change(找硬币)

    Level:   Medium 题目描述: You are given coins of different denominations and a total amount of money amo ...

  7. Video Mode Timings

    A monitor draws an image on the screen by using an electron beam (3 electron beams for color models, ...

  8. go语言从例子开始之Example18_1.结构体中定义方法

    Go 支持在结构体类型中定义方法 . Example: package main import "fmt" type product struct{ name string num ...

  9. java应用之solr入门篇

    前言 solr是apache项目的一款全文搜索应用. 官方文档http://lucene.apache.org/solr/guide/6_6/ 入门流程 1.安装   --->  2.启动  - ...

  10. spring 获取url参数

    1. usl格式: http://localhost:8080/contact/delete/3 java代码 @RequestMapping(value="/delete/{id}&quo ...