jQuery切换复选框 [英] Jquery Toggle Checkbox

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

问题描述

所以我有一个问题.我正在尝试切换复选框,以便将一些CSS添加到父<tr>

So I have an issue. I'm trying to toggle checkboxes in order to add some css to the parent <tr>

这是我到目前为止所拥有的

This is what I have so far

 $('input[type=checkbox]').toggle(function(){

    $(this).parents('tr').addClass('selectCheckBox', 970);
},
function(){

    $(this).parents('tr').removeClass('selectCheckBox', 970);
});

但是,这样做的结果是<tr>确实接收到selectCheckBox,但是未选中该复选框.所以我尝试了以下方法:

However, the result of this is that the <tr> does receieve the selectCheckBox but the checkbox does not become checked. So I tried the following:

 $('input[type=checkbox]').toggle(function(){
    $(this).attr('checked',true);
    $(this).parents('tr').addClass('selectCheckBox', 970);
},
function(){
    $(this).attr('checked',false);
    $(this).parents('tr').removeClass('selectCheckBox', 970);
});

再一次,没有运气.

我认为这与复选框ID的外观有关.

I believe it has to do with how the checkbox id looks.

<input type="checkbox" name="select[]" id="select[]">

然后我尝试使用过去使用的\\[\\]来转义[],但是没有运气.

I then tried escaping the [] by using \\[\\] as I've used in the past, but no luck.

是否可以检查checkbox?

推荐答案

我知道了.我需要将toggleClass与其他jQuery UI参数一起使用才能获得所需的效果.

I figured it out. I needed to use toggleClass along with the rest of jQuery UI parameters to get the effect needed.

$('input[type=checkbox]').click(function(){
    $(this).parents('tr').toggleClass('selectCheckBox',  1000, "easeOutSine" );
});

// Here are the parameteres below
    .toggleClass( className [, switch ] [, duration ] [, easing ] [, complete ] )

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

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