jquery附加所有值

时间:2017-02-10 06:47:16

标签: php jquery html

实际上我想只为div附加已检查的值,但它也附加了未经检查的值。我使用in_array函数来检查条件是否检查了值意味着只附加选中的值而不是全部但是它附加了所有值,但它只检查了值,请有人帮助我...

下面是HTML和PHP代码: -

> stars = { { x=1, y=1 }, { x=7, y=4 } }
> light_grid = light_universe(7, 32, stars, 1)
> show_intensities(light_grid, 7)
   Star      0.13085   0.05729   0.04587   0.04728   0.06073   0.13366
   0.14064   0.08582   0.05424   0.04640   0.04971   0.06411   0.09676
   0.09676   0.06411   0.04971   0.04640   0.05424   0.08582   0.14064
   0.13366   0.06073   0.04728   0.04587   0.05729   0.13085   Star   
   0.08469   0.05562   0.04574   0.04433   0.05218   0.08190   0.13222
   0.06715   0.05619   0.04631   0.04302   0.04635   0.05627   0.06728
   0.13073   0.08043   0.05075   0.04294   0.04439   0.05432   0.08347

Jquery代码: -

<div class="flDrop">
    <div class="flDropDiv">
        <?php 
        foreach ($filter_group['filter'] as $filter) { ?>
            <?php 
            if(in_array($filter['filter_id'],$filter_category))
            { ?>
                <input name="filter[]" type="checkbox" value="<?php echo $filter['filter_id']; ?>" checked> <?php echo $filter['name'];?> 
            <?php } else {?>
                <input name="filter[]" type="checkbox" value="<?php echo $filter['filter_id']; ?>"> <?php echo $filter['name'];?> 
            <?php }?>
        <?php }?> 
    </div>   
</div>  

1 个答案:

答案 0 :(得分:1)

通过一些小改动(示例代码)正常工作: -

&#13;
&#13;
setTimeout(function(){
    $.each($("input[name='filter[]']:checked"), function(){ //change here           
        $("#auto_filter_values").append('<div class="afr fSbtn" id="fSfilter'+this.value+'" data-val="'+this.value+'">'+$(this).attr('data-id')+'<span class="fSc"></span></div>'); // changes here
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="flDrop">
    <div class="flDropDiv">
        <input name="filter[]" type="checkbox" value="1" data-id ="Black" checked> Black <br>
        <input name="filter[]" type="checkbox" value="2" data-id ="Black">Red<br/>
        <input name="filter[]" type="checkbox" value="3" data-id ="Pink" checked>Pink<br/>
        <input name="filter[]" type="checkbox" value="4" data-id ="Blue">Blue<br/>
    </div>   
</div>

<div id = "auto_filter_values" style ="margin-top:30px;"></div>
&#13;
&#13;
&#13;

注意: - 您需要在复选框中添加data-id属性和颜色名称,然后才能轻松搞定