2013-02-20 134 views
0

Function;php jquery ajax append(html)in a loop

<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
jQuery(function($){ 
function detail(dataone) { 
     $.ajax({ 
     url: "detail.php?name=" + dataone, 
     cache: false 
     }).done(function(html) { 
     $("#detail").append(html); 
     }).fail(function(jqXHR, ajaxOptions, thrownError){ 
     }); 
    } 
}); 
    </script> 

循環在表中;

while ($row=mysql_fetch_array($query)) { 
echo "<tr onclick='detail(".$row['column1'].")'>"; 
    echo '<td scope="row">'.$row['column1'].'</td> 
     <td>'.$row['column2'].'</td> 
    <td>'.$row['column3'].'</td> 

    </tr>'; } 

循環的樣本輸出;

<tr onclick="detail(name)"> 
<td scope="row">name</td> 
<td>value</td> 
<td>value2</td> 
</tr> 

和html;

<div id="detail"></div> 

我只是想發送名稱到detail.php與獲取並返回html輸出到detail div。爲什麼這個功能不起作用? (什麼也沒有發生在TR上點擊,沒有錯誤)

detail.php

if (isset($_GET['name'])) { 
echo $_GET['name']; } 
else echo "Test"; 

編輯

更改的功能與這一點,它的工作。

var detail = function(dataone) 
{ 
    $.ajax({ url: "detail.php?name=" + dataone, cache: false 
    }).done(function(html) 
{ 
    $("#detail").append(html); 
}).fail(function(jqXHR, ajaxOptions, thrownError){ } 
); 
} 
+0

你得到什麼結果呢?你有錯誤嗎? – Stasel 2013-02-20 08:14:17

+0

@Stasel不,點擊上沒有任何反應。 – milesh 2013-02-20 08:16:11

+0

點擊什麼? – kraYz 2013-02-20 08:19:20

回答

0

拳所有的,我建議你通過數據作爲對象常量,使用應用於對象中的數據屬性$阿賈克斯:

data: { name: dataone }, 
dataType: 'html' //since you are fetching HTML 

而且因爲你可能是在結束了錯誤處理程序,現在,你可以添加一個檢查錯誤:

//... 
}).done(function(html) { 
    $("#detail").append(html); 
}).fail(function(jqXHR, ajaxOptions, thrownError){ 
    //handle error 
}); 

這將相當於

$.ajax({ 
//... 
error: function(jqXHR, ajaxOptions, thrownError){ 
    //handle error 
}, 
//... 

thrownError應該告訴你足夠的知道哪裏出了問題。

$(function(){ 

    $('.yourTr').click(function(){ 

      //add ajax stuff here 
    }); 

}); 
+0

我添加了.fail函數,但仍然沒有輸出。我嘗試了沒有數據,但仍然不工作 – milesh 2013-02-20 08:29:08

+0

@milesh請告訴我你是如何觸發ajax調用。聽起來你甚至沒有在單擊處理程序中結束 – Johan 2013-02-20 08:29:58

+0

我更新了代碼並添加了表格的輸出。 – milesh 2013-02-20 08:34:11

0

我認爲你必須在你的函數的一些錯誤:

jQuery(function($){ 
    function detail(dataone) { 
     $.ajax({ 
       url: "detail.php?name=" + dataone, 
       cache: false 
      }).done(function(html) { 
       $("#detail").append(html); 
      }); //<----------------------------------------this is extra 
      }).fail(function(jqXHR, ajaxOptions, thrownError){ 
      }); 
      }); 
     }; //<-------------------';' 
     //<-------------missing the closing doc ready 

而是嘗試將功能detail()doc ready之外或者只是remove the doc ready

function detail(dataone) { 
     $.ajax({ 
     url: "detail.php?name=" + dataone, 
     cache: false 
     }).done(function(html) { 
     $("#detail").append(html); 
     }).fail(function(jqXHR, ajaxOptions, thrownError){ 
     }); 
    } 
+0

耶剛剛更新了答案。 – Jai 2013-02-20 08:47:36

+0

編輯後發生的事情我認爲是固定的。現在我在控制檯中有錯誤;語法錯誤:缺失)在參數列表細節(名稱SURNAME)之後<指向這個「S」 – milesh 2013-02-20 08:54:49

+0

'細節(名稱,SURNAME)'這應該'','分離。 – Jai 2013-02-20 09:42:42