如何删除选中项=";勾选“;对于带有jQuery的未选中复选框?
这是我目前的剧本如何删除选中项=";勾选“;对于带有jQuery的未选中复选框?,jquery,Jquery,这是我目前的剧本 <script> $().ready(function(){ $('.prettycheckbox').click(function () { $('.prettycheckbox input').removeAttr('checked'); $('.prettycheckbox a').removeClass('checked
<script>
$().ready(function(){
$('.prettycheckbox').click(function () {
$('.prettycheckbox input').removeAttr('checked');
$('.prettycheckbox a').removeClass('checked');
$("a", this).addClass('checked');
$("input", this).addAttr("checked");
});
});
</script>
$().ready(函数()){
$('.prettycheckbox')。单击(函数(){
$('.prettycheckbox input').removeAttr('checked');
$('.prettycheckbox a').removeClass('checked');
$(“a”,this).addClass('checked');
$(“输入”,this).addAttr(“选中”);
});
});
但是它不能很好地工作,因为在链接中添加和删除类可以很好地工作,但是对于输入来说它不起作用
如何使“选中”复选框如下所示:
<input class="payment_type" type="checkbox" value="1" name="payment_type" style="display: none;" checked="checked">
其他的都是这样的吗
<input class="payment_type" type="checkbox" value="1" name="payment_type" style="display: none;">
怎么做?类似于使用jquery和复选框输入无线电类型?用于更改选中的值
$('.prettycheckbox input').prop('checked', false);
另外需要注意的是,方法addAttr
在中不存在
jquery1.6+
要更改复选框的属性,应使用函数
$('.prettycheckbox input').prop('checked', false);
$('.prettycheckbox input').prop('checked', true);
jQuery 1.5及以下版本
.prop()
函数不存在,但具有类似功能:
$('.prettycheckbox input').attr('checked', 'checked');
注意:removeAttr
有效,但addAttr
无效。试试这个
document.getElementById("check1").checked = true;
document.getElementById("check1").checked = false;
该方法的可能重复项是.attr(“checked”、“checked”)
,但它不是您想要的,因为属性“checked”
引用了。defaultChecked
属性,您想要更改。checked
。更改.defaultChecked
属性或“checked”
属性只有在用户尚未触摸复选框时才有效。由于某些原因,这是可行的;D$('.prettycheckbox输入').removeAttr('checked');$('.prettycheckbox a').removeClass('checked');$(“a”,this).addClass('checked');$(“输入”,this).attr(“选中”,true)
但这不取决于jquery版本,如果OP使用的是早于1.6的版本,他/她可能会使用.attr(),这是有效的:$('.prettycheckbox input')。removeAttr('checked');$('.prettycheckbox a').removeClass('checked');$(“a”,this).addClass('checked');$(“输入”,this).attr(“选中”,true)你能解释为什么吗?@michaelkonstincs:不清楚你想做什么。你似乎在尝试检查和取消检查同一项method@ClaudioRedi我认为我在问题中提供的代码是明确的。我的意思是单击(),然后删除,删除,添加,添加。嗯,没关系。这可能只适用于高级jquery用户。@michaelkonstincs:LOL是的,你的代码是我无法理解的。