代码之家  ›  专栏  ›  技术社区  ›  TH1981

使用Jquery基于select选项选中特定复选框

  •  1
  • TH1981  · 技术社区  · 15 年前

    我已经使用PHP预定义了变量,因此我的数组如下所示:

    var per =[]; 
    
    per['3'] = []; 
    per['3']['4'] = '4'; 
    per['3']['24'] = '24'; 
    per['3']['17'] = '17'; 
    
    per['4'] = []; 
    per['4']['null'] = 'null'; 
    
    per['6'] = []; 
    per['6']['19'] = '19';
    

    我已经设法使用一个警报来测试我是否可以提取正确的数据。我想这可能是我提到的复选框中的某个东西让我很痛苦。

    我的jquery代码:

        $("#get_boxes").change(function () {
        var per =[]; 
    
    per['3'] = []; 
    per['3']['4'] = '4'; 
    per['3']['24'] = '24'; 
    per['3']['17'] = '17'; 
    
    per['4'] = []; 
    per['4']['null'] = 'null'; 
    
    per['6'] = []; 
    per['6']['19'] = '19';
    
        var role = $(this).val();
            $.each(per[role], function(k, v){
                if(v != undefined) { 
                $('#'+v).attr('checked', true);
                }
            });
        });
    

    <select name="role" id="get_boxes" class="full-width">
        <option value="null">---</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
    <option value="6">Six</option>
    </select>
    <p class="halflist">
            <input type="checkbox" name="permission[1]" id="1" />
            <label for="1" class="inline">Tick One</label>
    </p><p class="halflist">
            <input type="checkbox" name="permission[4]" id="4" />
            <label for="4" class="inline">Tick Four</label>
    </p><p class="halflist">
            <input type="checkbox" name="permission[5]" id="5" />
            <label for="5" class="inline">Tick Five</label>
    </p><p class="halflist">
            <input type="checkbox" name="permission[6]" id="6" />
            <label for="6" class="inline">Tick Six</label>
    </p><p class="halflist">
            <input type="checkbox" name="permission[2]" id="2" />
            <label for="2" class="inline">Tick Two</label>
    </p>
    

    提前谢谢!

    2 回复  |  直到 15 年前
        1
  •  0
  •   Robert    15 年前

    在你的例子中,你想要这样的东西

    var ar = ['Odd','Even'];
    ar['Odd'] = [1,3,5,7,9];
    ar['Even'] = [0,2,4,6,8];
    $('select').change(function() {
        var t = $(this).val();
        $.each(ar[t], function(key,value) {
            $('#'+value).attr('checked',true);
        });
    });​
    

    http://jsfiddle.net/sUHNp/

        2
  •  1
  •   skolima    15 年前

    最好将类添加到依赖复选框中。例如,如果一个人选中了复选框a,然后复选框B、C和D被认为是自动选中的,则可以将一类复选框a添加到复选框B、C和D中。然后只需执行jQuery select on.check\U a。

    HTML。。。

    <input type="checkbox" id="A" />
    <input type="checkbox" id="B" class="check_A" />
    <input type="checkbox" id="C" class="check_A" />
    <input type="checkbox" id="D" class="check_A" />
    <input type="checkbox" id="E" />
    <input type="checkbox" id="F" class="check_E" />
    <input type="checkbox" id="G" class="check_E" />
    <input type="checkbox" id="H" class="check_E" />
    

    $('#A').change(function() {
        $('.check_A').attr('checked', 'checked');
    }