dongmei1988 2016-12-17 22:33
浏览 21
已采纳

试图在php循环中隐藏jquery中的特定div

I have this form being outputted from a PHP while loop :

echo '<div id="postCont" class="postCont'.$pid.'" style="block;">
    <div id="clLink">
      <a id="clLink" href="'.$plink.'" target="_blank"" title="'.$ptitle.'">'.$ptitle.'</a>
    </div>
    <div id="clDate">Posted on '.$pdate.'</div>
    <div id="clDesc">'.$pdesc.'</div>

    <form method="post" class="ibadForm">
        <input type="hidden" name="id" value="'.$pid.'">
        <input type="hidden" name="hiddenBad" value="yes">
        <input type="image" src="../img/bad.png" name="subBad" value="Bad" class="bad">
    </form>
</div>';

I am trying to remove the individual .postCont when the ibadForm is clicked with jquery.

$(".ibadForm").submit(function(e) {
     var url = "add_form.php"; 
     var id = <?php echo $pid?>;
     $.ajax({
          type: "POST",
          url: url,
          data: $(this).serialize(),
          success: function(data)
         {
             $('.postCont' + id).hide();
         }
     });
     e.preventDefault();
 });

It submits the form to add_form.php fine but doesn't hide the postCont. If I remove the id from the class then it hides all post Cont's. Can anyone tell me where I am going wrong?

  • 写回答

1条回答 默认 最新

  • dsogx84602 2016-12-17 22:34
    关注

    You could use closest() to get the parent div then hide it using hide() method like :

    $(".ibadForm").submit(function(e) {
      var url = "add_form.php"; 
      var id = <?php echo $pid?>;
      var _this = $(this);
    
      $.ajax({
        type: "POST",
        url: url,
        data: $(this).serialize(),
        success: function(data)
        {
          _this.closest('.postCont').hide();
        }
      });
      e.preventDefault();
    });
    

    NOTE : You should store the $(this) object that refer to the clicked form in some variable (_this in my example) then use it inside the success callback since $(this) inside callback doesn't refer no more to the form, e.g :

    _this.closest('.postCont').hide();
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 微信小程序蓝牙数据透传
  • ¥15 加氢站氢负荷数据集来源
  • ¥15 umi接入sentry遇到问题
  • ¥15 HBuilderX打包H5网页,扫码模块无法使用
  • ¥15 Javascript跳转页面后,无法执行后面代码,如何解决?
  • ¥15 echarts绘制图表
  • ¥15 请教两个关于高德地图定位不准的技术问题
  • ¥15 根据企业名称 对照两个文件 样本筛选/匹配
  • ¥15 Linux环境下CA证书更新问题
  • ¥15 sqlserver语句提取结果以外数据