douyu5775 2014-04-08 22:01
浏览 28
已采纳

jquery无法获取ajaxed数据?

On document load I run the following ajax script.

function LoadData() {             
  $.ajax({
    type: "GET",
    url: "display.php",             
    dataType: "html",                
    success: function(text){                    
        $("#responsecontainer").html(text); 
    }

});
}

Here is the PHP script it pulls the data from. Yes I know it's depreciated and most likely vunerable to attack.

<?php
include 'db.php';
$counter = 0;
echo '<table  class="table" id="tableShow">
<tr>
<td align=center><b>ID</b></td>
<td align=center><b>Name</b></td>
<td align=center><b>Quantity</b></td>
<td align=center><b>Price</b></td></td>
<td align=center><b>Description</b></td>
<td align=center><b>Edit Item</b></td>
';
$result = mysql_query("SELECT * from user ORDER BY `id` ASC");

while($data = mysql_fetch_row($result))
{   
    echo "<tr>";
    echo "<td align=center>$data[0]</td>";
    echo "<td align=center>$data[1]</td>";
    echo "<td align=center>$data[3]</td>";
    echo "<td align=center>$data[4]</td>";
    echo "<td align=center>$data[2]</td>";
    echo '<td align=center><a class="btn Edititem btn-info btn-small" id="'.++$counter.'">Test1</a></td>';
    echo "</tr>";
}
echo "</table>";

?>

As you can see, within the table it has a button for each row. <a class="btn Edititem btn-info btn-small" id="'.++$counter.'">Test1</a></td>

I then have this script.

  $(".Edititem").click(function () 
    {
    $('#Edit').modal('show');
      $("#updateResults").click(function (){
        $.ajax({                                      
          url: 'api.php',        
          data: "id="+ $(this).find('a').attr('id'),
          dataType: 'json',    
          success: function(data)
          { var id = data[0]; 
            var name = data[1];
            var desc = data[2];
            var quant = data[3];
            var price = data[4];
            $('#inner-title').html(name);
            $('#itemid').val(id);
            $('#Name').val(name);
            $('#quant').val(quant);
            $('#price').val(price);
            $('#desc').val(desc);
            $('#Edit').modal('hide');
            $('#success').alert();
          },
          error: function(){$("#failure").alert();}       
        }); 
      });
  }); 

It seems that the html thats pulled from the PHP script, is invisible to jQuery. For example, when you click on one of the buttons, it should launch the modal I have within my page, however, it don't happen and I get no console errors either. But if I just insert the button into the #responsecontainer like so:

<div id="responsecontainer">
  <a class="btn Edititem btn-info btn-small" id="'.++$counter.'">Test1</a>
</div>

jQuery can find it, and the modal launches?

What is wrong here?

  • 写回答

2条回答 默认 最新

  • duanpeng1532 2014-04-08 22:11
    关注

    I recommend you to use .on() jQuery function.

    .on() | jQuery API Documentation

    Consider following script update:

    $("#responsecontainer").on("click", ".Edititem", function() {
        $('#Edit').modal('show');
        $("#updateResults").click(function() {
            $.ajax({
                url: 'api.php',
                data: "id=" + $(this).find('a').attr('id'),
                dataType: 'json',
                success: function(data)
                {
                    var id = data[0];
                    var name = data[1];
                    var desc = data[2];
                    var quant = data[3];
                    var price = data[4];
                    $('#inner-title').html(name);
                    $('#itemid').val(id);
                    $('#Name').val(name);
                    $('#quant').val(quant);
                    $('#price').val(price);
                    $('#desc').val(desc);
                    $('#Edit').modal('hide');
                    $('#success').alert();
                },
                error: function() {
                    $("#failure").alert();
                }
            });
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败