简体   繁体   English

Jquery 复选框仅在选中取消选中然后选中时切换

[英]Jquery checkbox toggling only on checking unchecking and then checking

I want to show a div on checking a checkbox and hide the div on unchecking the checkbox.This is what I tried.It works only on checking and unchecking and then checking.我想在选中复选框时显示一个 div,并在取消选中复选框时隐藏 div。这是我尝试过的。它仅适用于选中和取消选中然后检查。 I am using jquery 3.5.1.我正在使用 jquery 3.5.1。

I think there is a problem with this jquery part logic我认为这个 jquery 部分逻辑有问题

jquery part jquery 零件

$('.frameopener').on('change', function(){ // on change of state
   if(this.checked) // if changed state is "CHECKED"
    {
       
           var formid=$(this).attr("id");
 
       $("#"+formid+"").click(function() {
   
  $("#" + formid + "form").toggle();
    
});

}
});

php part which dynamically displays checkbox from a loop php 部分从循环中动态显示复选框

echo '
 
  <td>
    <input type="checkbox" class="frameopener" id="'.$checkboxitemsafterstripper[$key].'" ></td><td>'.
   $item.'
  </td>';

Here is the frame which needs to be toggled这是需要切换的框架

foreach( $checkboxitemsafterstripper as $key => $item ){
echo '
<div id="'.$item.'form">';
}

I am not sure why you have use $("#"+formid+"").click(function() {.. there is no need to use this simply get id of checkbox and toggle div.我不知道你为什么使用$("#"+formid+"").click(function() {..没有必要使用这个简单地获取复选框的 id 并切换 div。

Demo code :演示代码

 $("div[id$=form]").hide() //hide all div whose id end with form $('.frameopener').on('change', function() { // on change of state var formid = $(this).attr("id"); $("#" + formid + "form").toggle(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" class="frameopener" id="12">12 <input type="checkbox" class="frameopener" id="13">13 <input type="checkbox" class="frameopener" id="14">14 <div id="12form">12 div</div> <div id="13form">13 div</div> <div id="14form">14 div</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM