简体   繁体   English

Yii框架:如何检查复选框是否被选中?

[英]Yii framework: how to check whether checkbox is checked?

I want always show additional fields when chceckbox is unchecked and always hide when is checked. 我想在未选中chceckbox时始终显示其他字段,而在选中时始终隐藏。

I have checkbox: 我有复选框:

<?php echo $form->checkBox($model2, 'ignore', array('onChange'=>'javascript:toggleUser2()')); ?>

js script: js脚本:

<script>
                    function toggleUser2() {
                        if($('.User2_ignore').is(':checked')) {
                            $('.user2').hide();
                        } else {
                            $('.user2').show();
                        }
                    }
                    toggleUser2();
                </script> 

This always return false: 这总是返回false:

 console.log($('.User2_ignore').is(':checked'));

I also tried attr('checked') and val(), but they are returnig 'undefined'. 我也尝试过attr('checked')和val(),但它们是returnig'undefined'。 Is there any way to check checkbox is checked or no? 有什么办法可以检查复选框是否被选中?

Try Using this Jquery Code as shown below 尝试如下所示使用此Jquery代码

$(document).ready(function(){
    $("input[type=checkbox]").change(function(){
        if($(this).is(":checked")){
            alert("Checked");
             //$(this).siblings("input[type=checkbox]").removeAttr("checked");
           }else{
            alert("Unchecked")  
           }
        });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM