在while循环中使用JQuery .validate

时间:2012-07-16 19:21:18

标签: jquery loops while-loop

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

$("#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>

1 个答案:

答案 0 :(得分:0)

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