码迷,mamicode.com
首页 > 其他好文 > 详细

checbox复选框实现radio单选框的单选功能

时间:2016-02-03 20:02:37      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

checbox复选框实现radio单选框的单选功能:
大家知道复选框可以一次选中多个,单选按钮每次只能够选中其中的一个,但是单选按钮比较霸道,你选中以后,只能够且必须选中其中一个,所有下面就通过checkbox复选框模拟实现单选按钮的功能,但是能够取消选中的项。
代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落</title>
<style>
span, input{
  float:left;
}
input{
  width:14px;
  height:14px;
}
span{
  margin-right:20px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(function(){ 
  $(":checkbox").click(function(){ 
    if($(this).attr("checked")!=undefined){ 
      $(this).siblings().attr("checked",false); 
    } 
  }) 
}) 
</script>
</head>
<body>
<div>
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
</div>
</body>
</html>

上面的代码实现了我们想要的功能,下面介绍一下它的实现过程。
一.代码注释:
1.$(function(){}),文档结构完全加载完毕再去执行函数中的代码。
2.$(":checkbox").click(function(){}),为checkbox复选框注册click事件处理函数。
3.if($(this).attr("checked")!=undefined){},判断当前复选框是否被选中,因为如果复选框没有被选中attr()函数的返回值是undefined,如果选中的话返回值checked。
4.$(this).siblings().attr("checked",false),如果当前点击的被选中了,那么就将其兄弟复选框全部取消选中。
二.相关阅读:
1.attr()函数可以参阅jQuery的attr()方法一章节。 
2.siblings()函数可以参阅jQuery的siblings()方法一章节。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=14449

更多内容可以参阅:http://www.softwhy.com/jquery/

checbox复选框实现radio单选框的单选功能

标签:

原文地址:http://www.cnblogs.com/nulifendou/p/5180415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!