douwen7905 2016-10-05 10:09
浏览 284
已采纳

如何使用Jquery显示和隐藏复选框文本

Jquery on time show and hide check box text if checked show the text if unchecked remove the text.

I am Trying to show the selected check boxes text at left side,and hide it when unchecked this is my from code what i have tried:

<div class="products-row">
    <?php $tq=$conn->query("select * from os_tiffen where tiffen_status=1 order by id_tiffen ASC");
        while ($tiffen = $tq->fetch_assoc()) {
        ?>  
        <div class="col-md-3"> 
            <div class="foodmenuform row text-center">
                <input type="checkbox" id="<?php echo $tiffen['tiffen_image'];?>" hidden>
                <label for="<?php echo $tiffen['tiffen_image'];?>"><img src="img/tiffen/<?php echo $tiffen['tiffen_image']; ?>" alt="" class="img img-responsive" /></label>
                <h3 id="tiffenname"><?php echo $tiffen['tiffen_name'];?></h3>
            </div>
        </div>
    <?php }  ?>
</div>

And this is My Script:

$( document ).ready(function() {
var FoodMenu = $('input[type=checkbox]:checked').map(function(){
return $(this).next('h3').text();
}).get().join("<br>");
$("#selectedfood").html(FoodMenu);
});

And this is the place where i am showing the output:

<div class="related-row" id="FoodSelected">
    <h4 class="text-center">Food Selected</h4> 
    <ul class="nav nav-pills nav-justified">
        <li class="active"><a id="selectedfood"></a></li>
    </ul>    
</div>
  • 写回答

2条回答 默认 最新

  • duanhuo3392 2016-10-05 10:14
    关注

    $(document).ready(function(){
    
    $("input:checkbox").click(function() {
            var output = "";
            $("input:checked").each(function() {
                output += $(this).next('span').text();
            }); 
            if (output != '')
             $(".dropdown").append('<li>'+output+'</li>');  
      }); 
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="dropdown-content">
    
      <ul class="dropdown"></ul>
    <ul>
      <li><input type="checkbox"/><span>one</span></li>  
      <li><input type="checkbox"/><span>two</span></li> 
      <li><input type="checkbox"/><span>three</span></li> 
      <li><input type="checkbox"/><span>four</span></li> 
      <li><input type="checkbox"/><span>five</span></li> 
      <li><input type="checkbox"/><span>six</span></li> 
      <li><input type="checkbox"/><span>seven</span></li> 
      <li><input type="checkbox"/><span>eight</span></li> 
      <li><input type="checkbox"/><span>nine</span></li> 
    </ul>
    </div>

    Try It Once

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler