【问题标题】:How to uncheck a checkbox when checked option is other than 'All'如何取消选中选中选项后的复选框是“全部”
【发布时间】:2015-01-07 22:12:38
【问题描述】:

我有多个从数据库中填充的复选框,除了一个“全部”复选框(用于在点击时选中/取消选中所有其他复选框)

  1. 当所有选项都被选中并且如果“全部”以外的任何选项未选中,则应取消选中“全部”复选框。

  2. 在选中所有选项外,除了“全部”然后'所有'后,应检查。如何进行?

我的代码:

<script>
    $(document).ready(function () {
        ('#check').append('<input type="checkbox"  id="checkAll" name="myCheckbox[]" value="All" > </input>' + "All" );
        //'datadb' is data from db in json array 
        //datadb={'apple','banana','orange'}

        $.each(datadb, function(i, fruit) {
            $('#check').append('<input type="checkbox" name="myCheckbox[]" class=".chk"  value="' + fruit + '" > </input>' + fruit );
            $('#check').append('<br/>');        
        }
    });
</script>

<script>            
    $(document).ready(function() {
        $("#checkAll").click(function () {
            $('input:checkbox').not(this).removeProp('checked');
        });
    });
</script>
<div id="check" onchange="testingclick()"></div>

如何填充满足上述1和2的函数

<script>
    function testingclick(){
        var $check_values = $("input[type=checkbox][name='myCheckbox[]']:checked");
        var $check_len = $check_values.length;
        var $total_len = $("input[type=checkbox][name='myCheckbox[]']").length;

        window.var_multiple_checked = $check_values.map(function(){  return "'" + this.value + "'";   }).get(); 
        temp_checked= window.var_multiple_checked;
    }
</script>

【问题讨论】:

标签: javascript jquery checkbox functional-programming


【解决方案1】:

对于这个代码。

$('input:checkbox').not(this).prop('checked', this.checked);

尝试使用 removeProp 代替

$('input:checkbox').not(this).removeProp('checked');

对于您的复选框的事件。与事件挂钩。

$(document).ready(function() {
    $('input:checkbox').click(function () {
        if ($(this).attr('id') === 'checkAll' && $(this).is(':checked')) {
            $('input:checkbox').not($(this)).removeProp('checked');
        } else {
            $('#checkAll').removeProp('checked');
        }
    });
});

【讨论】:

  • 已编辑。有什么建议来完成这个功能吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多