입력 확인란 결과에 대한 jQuery ajax 반전 또는 숨기기

Nov 28 2020

나는 jquery에 대한 좋은 지식이 없기 때문에 코드가 잘 작동하지만 문제는 확인란을 선택 취소하면 결과를 숨기는 것입니다. (데이터베이스에서 결과를 얻으려면 선택하십시오-이 결과 숨기기를 선택 취소하십시오)

catfd.php 코드

 <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'에서 결과를 가져옵니다.


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 코드입니다.

$(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);
        }
      });
    }
  });
});

답변

1 Swati Nov 29 2020 at 13:48

와 DIV 있는지 확인할 수 있습니다 class="catcfid"귀하의 DOM에 존재 여부를 사용 $(".main-area-courses-continer > ." + catcf).length == 0길이라면 0수단 다음 백엔드에서 불과 사용하여 해당 사업부를 보여 다른 것을 가지고 아약스 요청을 할 수있는 등의 사업부가없는 $("." + catcf).parent().show();경우 다른 unchecked사업부 사용하는 것을 숨기기 $("." + 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>

Ashikul Nov 29 2020 at 14:11

QUERY 만 변경하면 문제인 것 같습니다.

$sql = 'SELECT * FROM tablename Where cat_id = '.$catcfid.' And status = '1' order by p_id ASC';