使用 jquery 和 php 动态禁用某些复选框选项


Dynamically disabled some checkbox options using jquery and php

<input type="checkbox" name="pref[]" value="red//fuji" />Fuji
<input type="checkbox" name="pref[]" value="red//pinklady" />Pink Lady
<input type="checkbox" name="pref[]" value="red//reddelicious" />Red Delicious
<input type="checkbox" name="pref[]" value="green//grannysmith" />Granny Smith
<input type="checkbox" name="pref[]" value="green//goldendelicious" />Golden Delicious

我想动态禁用复选框组,例如,如果选中了其中一个青苹果复选框,则三个红苹果复选框将被禁用。我还希望选中的复选框即使在禁用后仍保持选中状态。

在我的真实场景中,我将从MySQL数据库中填充复选框选项,那么如何使用HTML,PHP和jQuery或任何其他方式来实现这一点呢?

你去吧

.HTML

<input type="checkbox" name="pref[]" value="red//fuji" data-type="red" />Fuji
<br/>
<input type="checkbox" name="pref[]" value="red//pinklady" data-type="red" />Pink Lady
<br/>
<input type="checkbox" name="pref[]" value="red//reddelicious" data-type="red" />Red Delicious
<br/>
<input type="checkbox" name="pref[]" value="green//grannysmith" data-type="green" />Granny Smith
<br/>
<input type="checkbox" name="pref[]" value="green//goldendelicious" data-type="green" />Golden Delicious
<br/>

JavaScript/jQuery

$(function () {
    function disableCheckboxes(color) {
        $('input[type="checkbox"]').prop('disabled', true);
        $('*[data-type="' + color + '"]').prop('disabled', false);
    };
    function enableCheckboxes() {
        $('input[type="checkbox"]').prop('disabled', false);
    }
    $('input[type="checkbox"]').on('click', function () {
        var $this = $(this);
        var color = $this.data('type');
        var x = $('*[data-type="' + color + '"]').prop('checked');
        if ($this.is(':checked')) {
            disableCheckboxes(color);
        } else if (!$('*[data-type="' + color + '"]').prop('checked')) {
            enableCheckboxes();
        }
    });
});

示例:http://jsfiddle.net/xd9xz8vg/4/

它有点混乱,但可以通过将一些jQuery选择器声明为变量来提高性能来清理它。

编辑:根据评论更新了答案。

尝试使用 -

<input class="red" type="checkbox" name="pref[]" value="red//fuji" />Fuji
<input class="red" type="checkbox" name="pref[]" value="red//pinklady" />Pink Lady
<input class="red" type="checkbox" name="pref[]" value="red//reddelicious" />Red Delicious
<input class="green" type="checkbox" name="pref[]" value="green//grannysmith" />Granny Smith
<input class="green" type="checkbox" name="pref[]" value="green//goldendelicious" />Golden Delicious
$(document).ready(function() {
   $('input[type="checkbox"]').on('click', function(){
     $('.green').each(function() {
        if ($(this).is(':checked')) {
           $('.red').each(function() {
              //$(this).prop('disabled', true);
              $(this).attr('disabled', true);
           })
        }
     })
   })
})

希望以下代码可以帮助您处理PHP,这些将从服务器/数据库填充数据,就像Jquery一样,这将处理任何客户端/用户交互

使用 PHP 禁用

<?php
$selected_fruit = 'red//pinklady'; // this is got from DB
$selected_group = explode('//', $selected_fruit);
$checkbox_options = '';
// following variable $array_of_checkbox_option as you say should be from DB
$array_of_checkbox_option = array(
array('val'=>'red//fuji', 'display_text'=>'Fuji'),
array('val'=>'red//pinklady', 'display_text'=>'Pink Lady'),
array('val'=>'red//reddelicious', 'display_text'=>'Red Delicious'),
array('val'=>'green//grannysmith', 'display_text'=>'Granny Smith'),
array('val'=>'green//goldendelicious', 'display_text'=>'Golden Delicious')
);
foreach($array_of_checkbox_option as $value){
    $group_name = explode('//', $value['val']);
    $checkbox_options .= '<input type="checkbox" name="pref[]" value="';
    $checkbox_options .= $value['val'].'"';
    $checkbox_options .= ($selected_fruit == $value['val']) ? ' checked ' : '';
    $checkbox_options .= ($group_name[0] == $selected_group[0]) ? ' disabled ' : '';
    $checkbox_options .= '/>'.$value['display_text'].'<br>';
}
echo $checkbox_options;
?>

使用 JavaScript 禁用

<script>
$(document).ready(function() {
    $( "input[name^='pref']" ).on('click', function(){
        if($(this).is(':checked')){
            var group_name = $(this).val().split('//');
            var sel_group = group_name[0];
            $( "input[name^='pref']" ).each(function(){
                var elm_name = $(this).val().split('//');
                if(sel_group == elm_name[0]){
                    $(this).prop('disabled', true);
                }
            });
        }
    });
});
</script>