使用jQuery切换时未选中复选框 [英] Checkbox not getting checked when using jquery toggle

查看:67
本文介绍了使用jQuery切换时未选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在使用jquery切换功能时选中该复选框.当我使用.bind('click')时,效果很好.

I want the checkbox to be checked when I am using jquery toggle function. It works fine when I use the .bind('click').

$('#chkb').toggle(function () {

    $('#va').text("checked");
    $('#chkb').attr('checked', 'checked');

}, 
function () {

    $('#chkb').attr('checked', 'false');
    $('#va').text("");

});

html

<input type="checkbox" id="chkb" />

我如何完成它.

谢谢 吉恩

推荐答案

您是否真的必须在代码中checked? (是否有我错过的东西?单击复选框,已经切换了它的选中属性值.)

do you really have to checked it in code? (Is there something I missed? checkbox when clicked, already toggle it's checked attribute value.)

更好的方法是使用这样的更改

Better way to do this is by using change like this,

$('#chkb').change(function () {
    var self = this;
    $('#va').text(function(i,text){
        return self.checked?"checked":"";
    });
})

这篇关于使用jQuery切换时未选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆