iCheck检查复选框是否已选中

时间:2013-12-23 02:40:19

标签: javascript jquery checkbox icheck

我正在使用iCheck插件来自定义复选框。我需要在选中一个或多个复选框时显示某些文本,并在未选中任何文本时隐藏文本。

我当前的代码在第一次点击时显示文字,但除非我再点击2次,否则不会隐藏它。 我有多个复选框,如果选中其中一个,则显示文本,否则隐藏文本。 有人有什么想法吗?插件有:

ifChecked
ifChanged
ifClicked
ifUnchecked
ifToggled
ifDisabled
ifEnabled.......

回调....这是插件函数

$('input').iCheck({ 
checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue',
increaseArea: '20%' // optional
});

这是我试过的......

$('input').on('ifChecked', function(event){
$(".hide").toggle();
});

HTML

<input type="checkbox">
<div class"hide" style="display:none">Hi there</div>

15 个答案:

答案 0 :(得分:53)

对于那些与之斗争的人:

const value = $('SELECTOR').iCheck('update')[0].checked;

这会直接将truefalse作为boolean返回。

答案 1 :(得分:26)

您可以通过

获取复选框和状态的值
$('.i-checks').on('ifChanged', function(event) {
    alert('checked = ' + event.target.checked);
    alert('value = ' + event.target.value);
});

答案 2 :(得分:21)

检查一下:

var checked = $(".myCheckbox").parent('[class*="icheckbox"]').hasClass("checked");

if(checked) {
  //do stuff
}

答案 3 :(得分:13)

您只需要使用文档中的回调: https://github.com/fronteed/iCheck#callbacks

$('input').on('ifChecked', function(event){
  alert(event.type + ' callback');
});

答案 4 :(得分:11)

此处记录了所有回调和函数:http://fronteed.com/iCheck/#usage

$('input').iCheck('check'); — change input's state to checked
$('input').iCheck('uncheck'); — remove checked state
$('input').iCheck('toggle'); — toggle checked state
$('input').iCheck('disable'); — change input's state to disabled
$('input').iCheck('enable'); — remove disabled state
$('input').iCheck('indeterminate'); — change input's state to indeterminate
$('input').iCheck('determinate'); — remove indeterminate state
$('input').iCheck('update'); — apply input changes, which were done outside the plugin
$('input').iCheck('destroy'); — remove all traces of iCheck

答案 5 :(得分:6)

我写了一些简单的事情:

icheck初始化为:

$('input').iCheck({
    checkboxClass: 'icheckbox_square-blue',
    radioClass: 'iradio_square-blue',
    increaseArea: '20%' // optional
});

在其下添加此代码:

$('input').on('ifChecked', function (event){
    $(this).closest("input").attr('checked', true);          
});
$('input').on('ifUnchecked', function (event) {
    $(this).closest("input").attr('checked', false);
});

在此之后,您可以轻松找到原始复选框的状态。 我在icheck中使用gridView编写了此代码,并通过C#从服务器端访问其状态。

只需从 ID 中找到您的复选框。

答案 6 :(得分:5)

要知道是否已选中iCheck框

var isChecked = $("#myicheckboxid").prop("checked");

答案 7 :(得分:4)

将此代码用于iCheck:

formatter = logging.Formatter("%(asctime)-15s :: %([levelname])8s ::  %(message)s")

答案 8 :(得分:3)

呼叫

element.iCheck('update');

获取元素

的更新标记

答案 9 :(得分:2)

使用以下代码检查是否使用单一方法检查iCheck。

$('Selector').on('ifChanged', function(event){

    //Check if checkbox is checked or not
    var checkboxChecked = $(this).is(':checked');

    if(checkboxChecked) {
        alert("checked");
    }else{
        alert("un-checked");
    }
});

答案 10 :(得分:1)

void myPrint(const char* fmt, ...)
{
    char buffer[512] = {0,};

    va_list arg;
    va_start(arg, fmt);

    int r = vsnprintf(buffer, 511, fmt, arg); // buffer size is given
    if (r > 0)              // works correctly
        buffer[r+1] = '\0'; // crash because r is 200,000
    va_end(arg);
}

int main(int, char**)
{
    const char * data = "abcdefg...." // assuming that a length is 200,000 byte string
    myPrint("%s\n", data);
}

答案 11 :(得分:0)

这对我有用......试试吧

&#13;
&#13;
return
&#13;
&#13;
&#13;

答案 12 :(得分:0)

您可以将所有复选框都包装在父类中,并检查.checked ..

的长度
if( $('.your-parent-class').find('.checked').length ){
  $(".hide").toggle();
}

答案 13 :(得分:0)

使用此方法:

$(document).on('ifChecked','SELECTOR', function(event){
  alert(event.type + ' callback');
});

答案 14 :(得分:0)

如果有人对多个 icheck()感兴趣。然后,您可以使用下面的一个:

$("#mother_checkbox").on("ifChanged", function(){
        if($(this).is(':checked')) {
            $('.child_checkbox').prop('checked', true);
            $('.child_checkbox').iCheck('update');
        }
        else{
            $('.child_checkbox').prop('checked', false);
            $('.child_checkbox').iCheck('update');
        }
});

它将选中/取消选中所有子复选框。 美好的一天 !

相关问题