1

我遇到了一个问题,我在 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>
4

1 回答 1

0

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

于 2012-07-16T19:45:13.480 回答