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?

图片转代码服务由CSDN问答提供 功能建议

我从PHP循环输出此表单:

 <  code> echo'&lt; div id =“postCont”class =“postCont”。$ pid。'“style =”block;“&gt; 
&lt; div id =”clLink“&gt; 
&lt; a id =  “clLink”href =“'。$ plink。'”target =“_ blank”“title =”'。$ ptitle。'“&gt;'。$ ptitle。'&lt; / a&gt; 
&lt; / div&gt; \  n&lt; div id =“clDate”&gt;发布于'。$ pdate。'&lt; / div&gt; 
&lt; div id =“clDesc”&gt;'。$ pdesc。'&lt; / div&gt; 
 \  n&lt; form method =“post”class =“ibadForm”&gt; 
&lt; input type =“hidden”name =“id”value =“'。$ pid。'”&gt; 
&lt; input type =  “hidden”name =“hiddenBad”value =“yes”&gt; 
&lt; input type =“image”src =“../ img / bad.png”name =“subBad”value =“bad”class =“  bad“&gt; 
&lt; / form&gt; 
&lt; / div&gt;'; 
   
 
 

我正在尝试删除单个 .postCont 当用jquery点击 ibadForm 时。

  $(“。ibadForm”)。submit(function(e){
 var url  =“add_form.php”; 
  var id =&lt;?php echo $ pid?&gt ;; 
 $ .ajax({
 type:“POST”,
 url:url,
 data:$(this).serialize(),
  success:function(data)
 {
 $('。postCont'+ id).hide(); 
} 
}); 
 e.preventDefault(); 
}); 
   
 
 

它将表单提交给 add_form.php ,但不隐藏 postCont 。 如果我从类中删除了id,那么它会隐藏所有的后续Cont。 谁能告诉我哪里出错了?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    点赞 评论

相关推荐 更多相似问题