处理POST请求

时间:2011-03-30 20:47:00

标签: php javascript jquery

我正在尝试发布一些数据,但我没有实现我需要的东西。

无论我点击什么list-item,它都会发布最高list-item的值。

这是jquery函数:

$(".list-item-wrap a").click(function(){
    $.post("display2.php", { 
        msg_id: $(".list-item-wrap a .list-item").find('input').val()},
        function(data) {
            $("#list").load("display2.php", {
                msg_id: $(".list-item-wrap a .list-item").find('input').val()
            });
        //window.location.replace("display2.php");
    })
})

这是实际的html / php数据:

<?php

$query = "SELECT * from workflow WHERE name = '$username' ORDER BY msg_id DESC";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());

while ($row = mysql_fetch_object($result)) {
    echo "<div class=list-item-wrap>";
    echo "<a href='#'><div class=list-item><input class=msgid type=hidden value="
              .$row->msg_id.">";
    echo "<b><h1>".$row->subject."</h1></b>".substr($row->message, 0, 200)
              ."...<br><b>".$row->sender."</b>";
    echo "</div></a></div>";
}
?>

我哪里错了?

5 个答案:

答案 0 :(得分:2)

当你在一组元素上调用val()时,它只会返回第一个元素的值。您只需要在所需的元素上调用val()

请尝试使用此功能,以获取您点击的项目:

$(".list-item input", this).val()

在这种情况下,this是您想要的.list-item-wrap a,因此您需要获得input之后的信息。

或者,如果您要发布所有项目,请使用:

$('input', '.list-item-wrap a .list-item').serialize()

答案 1 :(得分:1)

你需要在锚点击上使用preventDefault():

$(".list-item-wrap a").click(function(e){
    e.preventDefault();
    $.post("display2.php", {msg_id: $(".list-item-wrap a .list-item").find('input').val()},
    function(data){
        $("#list").load("display2.php", {msg_id: $(".list-item-wrap a .list-item").find('input').val()});
        //window.location.replace("display2.php");
    })
})

答案 2 :(得分:0)

在功能结束时使用return false。像:

$(".list-item-wrap a").click(function(){
    $.post("display2.php", {msg_id: $(".list-item-wrap a .list-item").find('input').val()},
    function(data){
        $("#list").load("display2.php", {msg_id: $(".list-item-wrap a .list-item").find('input').val()});
        //window.location.replace("display2.php");
    })
    return false;
})

答案 3 :(得分:0)

对不起我误解了这个:

$(".list-item-wrap a").click(function(){
    $.post("display2.php", {msg_id: $(this).find('input').val()},
    function(data){
        $("#list").load("display2.php", {msg_id: $(".list-item-wrap a .list-item").find('input').val()});
        //window.location.replace("display2.php");
    })
})

答案 4 :(得分:0)

首先,您需要找到所需输入元素的值。
所以用

限制你的选择器
msg_id: $("input",this).val()

您还应该将回调方法更改为仅显示数据而不是执行新的ajax调用。

function(data) {
            $("#list").html(data);
}
相关问题