【发布时间】:2021-03-10 23:56:51
【问题描述】:
我对 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);
}
});
}
});
});
【问题讨论】:
-
嗨,你可以显示
main-area-courses-continerdiv 吗?此外,对于所有选中的帖子,内容都加载到此 div 中,您只需要从该 div 中删除或隐藏特定内容吗? -
警告!您对SQL injection 攻击敞开大门!您应该使用参数化的prepared statements,而不是直接在查询中使用完全未转义的用户数据。 永远永远永远相信用户输入。
-
Swati , main-area-courses-continer 就像数据库内部的 coniner echo 结果,我只需要为每个隐藏特定复选框的结果
-
我明白了,您如何区分它们的任何 id 或值?
-
我得到了结果输出(title namesecond name),现在我不会隐藏所有类2 通过取消选中特定的复选框并且值为 2 .. 我在 else if ($('input.catcf').is(":not(:checked)")){ $( "."+Number( $(this).val())).remove(); } 工作,但工作不好后选择了 multitply 选择不好,因为没有更新 $( "."+Number($(this).val()))