2011-03-30 205 views
1

我想發佈一些數據,但我沒有實現我需要什麼。處理POST請求

無論什麼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>"; 
} 
?> 

我要去哪裏錯了?

+1

是什麼問題? – Neal 2011-03-30 20:48:27

回答

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"); 
    }) 
}) 
+0

或返回false;最後。同樣的事情 – Jase 2011-03-30 20:50:47

+0

沒有幫助.. – amit 2011-03-30 20:51:56

+0

我downvoted,因爲這不是問題在這裏。我已經刪除它。 – 2011-04-01 16:34:02

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; 
}) 
+0

請參閱UPDATE。 – amit 2011-03-30 20:55:26

+0

嘗試將發送請求中的'msg_id'變量改爲this.children('。msgid')[0] .val()'。 – RDL 2011-03-30 21:00:46

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"); 
    }) 
}) 
+0

我不明白..爲什麼我應該推一個數組?我只需要當前列表項的值? – amit 2011-03-30 20:59:32

+0

我誤解你正在尋找最近的(愚蠢的閱讀理解)。嘗試找到$(this)的輸入。之前的方式是尋找所有的投入,並抓住它看到的第一個價值。 – scrappedcola 2011-03-30 21:06:20

2

當您在一組元素調用val(),它只會返回第一個1的值。您只需致電val()即可。

試着用這個代替,讓你在點擊的項目:

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

在這種情況下,this.list-item-wrap a你想要的,所以你需要它後得到input

或者,如果你想發佈的所有項目,使用:

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

首先,你需要找到你想要的輸入元素的值..
所以用

限制你選擇
msg_id: $("input",this).val() 

你也應該改變你的回調方法只顯示數據,而不是做一個新的Ajax調用..

function(data) { 
      $("#list").html(data); 
} 
+0

沒有幫助.. – amit 2011-03-30 21:12:18

+0

@amit,那麼請向我們顯示您的HTML,所以我們知道你想做什麼.. – 2011-03-30 22:07:39