0

功能;

<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>

我只想通过get 将名称发送到detail.php 并将html 输出返回到detail div。为什么这个功能不起作用?(点击 tr 没有任何反应,没有错误)

详细信息.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){ }
);
}
4

2 回答 2

0

首先,我建议您使用 $.ajax 中使用的对象中的 data 属性将数据作为对象文字传递:

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

});
于 2013-02-20T08:22:31.510 回答
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){
      });
   }
于 2013-02-20T08:44:03.560 回答