jquery控制checkbox单选 并取消

bleedinging 2009-07-31 11:25:48
例如一个checkbox击后 点击后 单选很容易 实现, 可惜 选择一个checkbox后 必须点别的checkbox才能取消 点自己不能取消 ,我的代码是这样的
var nid=-1;
("input[name='xg']").bind("click",function(){
if(nid==this.id&&$(this).attr("checked")==true)
{
$(this).attr("checked", false);
// alert(id);
// alert(this.id);
// alert('a');
}
if(nid==this.id&&$(this).attr("checked")==false)
{
$(this).attr("checked", false);
alert(nid);
alert(this.id);
alert('b');
}
if(nid!=this.id&&$(this).attr("checked")==false)
{

$("#"+nid).attr("checked", false);
alert($("#"+nid).val());
$(this).attr("checked", true);
alert(nid);
alert(this.id);
alert('c');
}
else if(nid!=this.id && $(this).attr("checked")==true)
{
$("#"+nid+"").attr("checked",false);

$(this).attr("checked", true);
// alert(nid);
// alert(this.id);
// alert('d');
}


就是用uid记录上一次点击的id 第2次点击时 判断和上一次点击是不是点的同一个checkbox 是就把状态设置为true,不是也把上一次点击的设置为false,单身吧这次点击的设置为true
...全文
1822 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
z470208239 2012-06-07
  • 打赏
  • 举报
回复
不错。。
qinjames 2011-07-11
  • 打赏
  • 举报
回复
上面少加了name
我这个不用锁定ID的
<input type="checkbox" name="check" onclick="checkCanel(this)" />A
<input type="checkbox" name="check" onclick="checkCanel(this)" />B
<input type="checkbox" name="check" onclick="checkCanel(this)"/>C
function checkCanel(Obj){
$("input[name='check']").each(function(){
$(this).attr("checked",false);
});
Obj.checked=true;
}
qinjames 2011-07-11
  • 打赏
  • 举报
回复
我这个不用锁定ID的
<input type="checkbox" onclick="checkCanel(this)" />A
<input type="checkbox" onclick="checkCanel(this)" />B
<input type="checkbox" onclick="checkCanel(this)"/>C
function checkCanel(Obj){
$("input[name='checkbox']").each(function(){
$(this).attr("checked",false);
});
Obj.checked=true;
}
sohighthesky 2009-08-04
  • 打赏
  • 举报
回复
 <input type="checkbox" onclick="hehe(this)" id="chk1"/><label for="chk1">哎 </label>
<input type="checkbox" onclick="hehe(this)" id="chk2"/><label for="chk2">哎 </label>
<input type="checkbox" onclick="hehe(this)" id="chk3"/><label for="chk3">哎 </label>
<input type="checkbox" onclick="hehe(this)" id="chk4"/><label for="chk4">哎 </label>
<input type="checkbox" onclick="hehe(this)" id="chk5"/><label for="chk5">哎 </label>


<script language="JavaScript">
var id="";
function hehe(obj) {
if(id!="" && id!=obj.id) {
document.getElementById(id).checked=false;
}
id=obj.id;
}
programhjh 2009-08-04
  • 打赏
  • 举报
回复
太炫了。支持jquery
bleedinging 2009-08-04
  • 打赏
  • 举报
回复
我的代码好像没错的 后来发现是因为checkbox是在服务端用stringbuilder画上去的原因,后来我改成直接脱上去的checkbox 问题就解决了
mark620 2009-08-03
  • 打赏
  • 举报
回复
循环遍历所有的CheckBox控件,看看是不是Checked状态。然后再做判断并赋值,选中需要打钩的。
jamm1986 2009-08-03
  • 打赏
  • 举报
回复
checked属性移除不好吧?
lookher 2009-08-02
  • 打赏
  • 举报
回复
checkbox.checked=true or false ,这个应该就能搞定
hookee 2009-07-31
  • 打赏
  • 举报
回复
checkbox点自己可以取消呀,radio才要点其他的吧
要实现什么效果?
bleedinging 2009-07-31
  • 打赏
  • 举报
回复
帮忙啊
bleedinging 2009-07-31
  • 打赏
  • 举报
回复
哎 自己顶顶
bleedinging 2009-07-31
  • 打赏
  • 举报
回复
大家帮我看看啊 困扰了我一天了
kuku_1 2009-07-31
  • 打赏
  • 举报
回复
if($("#ddd").attr("checked")=="false")
{
$("#ddd").attr("checked","true")
}
else
{
$("#ddd").attr("checked","false")
}
chenjianyong94 2009-07-31
  • 打赏
  • 举报
回复
var tmp = null;
$(function(){
$("input[name='xg']").bind("click",function(){
if(tmp!=null){
tmp.removeAttr("checked");
}
tmp = $(this);
});
}) ;
eugenepada 2009-07-31
  • 打赏
  • 举报
回复

var tmp = null;
$(function(){
$("input[name='xg']").bind("click",function(){
if(tmp!=null){
tmp.removeAttr("checked");
}
tmp = $(this);
});
}) ;




<input type="checkbox" name="xg">1 <br />
<input type="checkbox" name="xg">2 <br />
<input type="checkbox" name="xg">3 <br />
<input type="checkbox" name="xg">4 <br />
<input type="checkbox" name="xg">5 <br />

52,793

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧