反转或隐藏jQuery ajax以获取输入复选框结果 [英] Reverse or hide jQuery ajax for input checkbox results

查看:186
本文介绍了反转或隐藏jQuery ajax以获取输入复选框结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对jquery并不了解,所以我的代码工作得很好,但是他们的问题是,当取消选中复选框时隐藏结果.(检查从数据库中获取结果-取消选中隐藏这些结果)

I don't have good knowledge about jquery, so I have a code working good but their problem, is to hide the result when uncheck the checkbox .( check to get the results from the database - uncheck hide these results )

catfd.php代码

catfd.php code

 <input type="checkbox" value="2" class="catcf"> catfd2 <br />
 <input type="checkbox" value="35" class="catcf"> catfd35 <br />
 <input type="checkbox" value="22" class="catcf"> catfd22 <br />
 <input type="checkbox" value="133" class="catcf"> catfd133 <br />
 <input type="checkbox" value="28" class="catcf"> catfd28 <br />
 <input type="checkbox" value="33" class="catcf"> catfd33 <br />
 <input type="checkbox" value="55" class="catcf"> catfd55 <br />
 <input type="checkbox" value="44" class="catcf"> catfd44 <br />
 <div class="main-area-courses-continer">
<!-- here will echo the results -->

</div>

jquery ajax代码从PHP页面"getvalue.php"获取结果

jquery ajax code get the result from PHP page 'getvalue.php'


if(isset($_POST['catcf'])){
?>
<div class="main-area-courses-continer" >
<?php
    $catcfid= $_POST['catcf'];
    $sql = 'SELECT * FROM tablename  where cat_id = '.$catcfid.' order by p_id asc';
    $catandproid = $wpdb->get_results($sql);
    foreach($catandproid as $key){
/////////////////////////////////Updates
?>
<div class="<?php echo $catcfid; ?>" >
<?php
        echo $key->title;
?>
</div><!-- End of Continer catcfid -->
<?php
////////////////////////////////////////////
    }
}
?>
</div>

这是ajax jQuery代码

and the is the ajax jquery code

$(document).ready(function() {
  $(".catcf").on('click', function() {
    if ($('input.catcf').is(':checked')) {
      var catcf = Number($(this).val());
      $.ajax({
        url: 'getvalue.php',
        type: 'post',
        data: {
          catcf: catcf
        },
        beforeSend: function() {
          $(".main-area-courses-continer").html("Looding....");
        },
        success: function(response) {
          // Setting little delay while displaying new content
          setTimeout(function() {
            // appending posts after last post with class="post"
            $(".main-area-courses-continer:last").after(response).show().fadeIn("slow");
            $(".main-area-courses-continer").html("");
          }, 2000);
        }
      });
    }
  });
});

推荐答案

您可以检查DOM中是否存在具有 class ="catcfid" 的div,或者不使用 $("; .main-area-courses-continer>."+ catcf).length == 0 如果长度为 0 表示没有这样的div,则可以发出ajax请求要将其从后端导入,则只需使用 $(." + catcf).parent().show(); 显示该div,否则,如果 unchecked 隐藏该div使用 $(." + catcf).parent().show(); .

You can check if the div with class="catcfid" exist in your DOM or not using $(".main-area-courses-continer > ." + catcf).length == 0 if the length is 0 means there is no such div then you can make ajax request to bring that from backend else just show that div using $("." + catcf).parent().show(); else if unchecked hide that div using $("." + catcf).parent().show(); .

演示代码 :

$(document).ready(function() {
  $(".catcf").on('click', function() {
    var catcf = Number($(this).val());
    if ($(this).is(':checked')) {
      //check if the div with class catcf is not there 
      if ($(".main-area-courses-continer >  ." + catcf).length == 0) {
        /*.ajax({
           url: 'getvalue.php',
           type: 'post',
           data: {
             catcf: catcf
           },
           beforeSend: function() {
             $(".main-area").html("Looding....");
           },
           success: function(response) {*/
        setTimeout(function() {
          //$(".main_container:last").after(response).show().fadeIn("slow");
          //dummy data append ..
          $(".main_container:last").after('<div class="main-area-ourses-continer"><div class="' + catcf + '">catfd' + catcf + ' data....</div> </div>')
          $(".main-area").html("")//empty loading ..
        }, 100);
        /*
        });*/

      } else {
        //show that class 
        $("." + catcf).parent().show();
      }
    } else {
      //hide that class means unchecked
      $("." + catcf).parent().hide()
    }
  });
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" value="2" class="catcf" checked> catfd2 <br />
<input type="checkbox" value="35" class="catcf"> catfd35 <br />
<input type="checkbox" value="22" class="catcf" checked> catfd22 <br />
<input type="checkbox" value="133" class="catcf"> catfd133 <br />
<input type="checkbox" value="28" class="catcf"> catfd28 <br />
<input type="checkbox" value="33" class="catcf"> catfd33 <br />
<input type="checkbox" value="55" class="catcf" checked> catfd55 <br />
<input type="checkbox" value="44" class="catcf"> catfd44 <br /> Datas :
<!--put this div for loading separtely-->
<div class="main-area"></div>

<div class="main_container">
  <div class="main-area-courses-continer">
    <div class="2">
      catfd2 data....
    </div>
  </div>
  <div class="main-area-courses-continer">
    <div class="22">
      catfd22 data....
    </div>
  </div>
  <div class="main-area-courses-continer">
    <div class="55">
      catfd55 data....
    </div>
  </div>
</div>

这篇关于反转或隐藏jQuery ajax以获取输入复选框结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆