2012-07-16 116 views
1

我有一个问题,我在while循环中有窗体,我试图使用JQuery提交它们以从mysql数据库删除条目。问题是,当我按删除时,无论按下哪个删除按钮,它都会发送相同的ID。在while循环中的JQuery .validate

$("#deletePost").validate({ 
     debug: false, 
     rules: {   
     }, 
     //If the rules are not met, the following messages are displayed beside the input field. 
     messages: { 
     }, 
     // do other stuff for a valid form 
     submitHandler: function(form) { 
      $.post('bin/Profile.php', $("#deletePost").serialize(), function(data) { 
       $('#ProfileWall').prepend(data); 
      }); 
     } 
    }); 
}); 

      <div id='ProfileWall'> 
      <?php 

       $WallRequest = mysql_query("SELECT * from WallPost where WallOf = '".$sn."' group by TimeAndDate DESC;")or die(mysql_error()); 
       while($WallInformation = mysql_fetch_array($WallRequest)) 
       { 
        $PostedBy = mysql_fetch_array(mysql_query("SELECT * from members where ServiceNumber = '".$WallInformation['PostedBy']."';")); 
      ?>  

         <div id = "post_<?php echo $WallInformation["id"]; ?>" >  
         <form name = 'deletePost' id = 'deletePost' method = 'post'> 
         <table border = '0' width = '550' height = '60' valign = 'top' align = 'center'> 
          <tr valign = 'center'> 
           <td width = '10' valign = 'center'> 
            <input type = "hidden" name = "id" id = "id" value = "<?php echo $WallInformation['id']; ?>" > 
            <input type = "hidden" name = "sn" id = "sn" value = "<?php echo $sn; ?>" > 
            <input type = "submit" name="Delete" id = "Delete" value="Delete" class="delete" /> 
           </td> 
          </tr> 
         </table> 
         </form> 
         </div> 

      <?php } ?> 
     </div> 

回答

0

的问题是,你总是在序列化的submitHandler相同的形式。您可以考虑在标记(<form id="$generate_a_unique_id_and_put_it_here" class="deletePost">)中为每个条目指定一个具有相同类别的唯一标识,然后使用$(this)仅选择要在submitHandler中提交的.deletePost实例,而不是通过标识选择(如$('#deletePost'))。

+0

我试过了,但没有奏效。其他人能否更好地解释我如何能够完成这项工作。谢谢 – 2012-07-16 21:32:21