【问题标题】:Dynamically disabled some checkbox options using jquery and php使用 jquery 和 php 动态禁用一些复选框选项
【发布时间】:2015-02-02 06:02:58
【问题描述】:
<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 或任何其他方式来实现呢?

【问题讨论】:

标签: javascript php jquery html checkbox


【解决方案1】:

给你

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 选择器声明为变量来提高性能。

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

【讨论】:

  • 谢谢这个工作,但是除了使用类(首选值)来选择禁用哪个复选框之外,还有其他方法吗?
  • 当然可以使用 HTML5s 自定义数据属性来存储颜色。我更新了答案
【解决方案2】:

试试 -

<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);
           })
        }
     })
   })
})

【讨论】:

  • 它不会禁用任何选项:(
  • 抱歉,仍然没有禁用任何选项。
【解决方案3】:

希望下面的代码可以帮助您处理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>

【讨论】:

    猜你喜欢
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多