Jquery on time show and hide check box text if checked show the text if unchecked remove the text.
我正在尝试在左侧显示所选复选框文本,并在未选中时隐藏它 这是我的代码,我尝试过:
<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>
这是我的剧本:
$( document ).ready(function() {
var FoodMenu = $('input[type=checkbox]:checked').map(function(){
return $(this).next('h3').text();
}).get().join("<br>");
$("#selectedfood").html(FoodMenu);
});
这是我展示输出的地方:
<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>
答案 0 :(得分:3)
$(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>
尝试一次
答案 1 :(得分:1)
请仔细查看您的布局,标签里面没有名字。可能的解决方案是将菜肴名称包装在容器中:
<div class="col-md-3">
<div class="foodmenuform row">
<input type="checkbox" id="<?php echo $lunch['id_lunch'];?>" />
<span class="foodmenuform-name"><?php echo $lunch['lunch_name'];?></span>
<label for="<?php echo $lunch['id_lunch'];?>"><img src="img/lunch/<?php echo $lunch['lunch_image']; ?>"/></label>
</div>
</div>
然后使用此容器获取菜名:
$( document ).ready(function() {
//console.log( "ready!" );
var FoodMenu = $('input[type=checkbox]:checked').map(function(){
return $(this).next('.foodmenuform-name').text();
}).get().join("</li><li>");
$("#selectedfood").html("<ul><li>" + FoodMenu + "</li></ul>");
});