使用jquery multiple div加载数据

时间:2013-11-16 00:29:28

标签: javascript php jquery

我有以下jquery函数从数据库加载数据工作正常,但我想在同一页面上再次显示相同的数据,当我这样做它不起作用。它只会显示第一个。有人可以帮帮我吗?

     <script type="text/javascript">
  $(document).ready(function() {
    loadData();
});

var loadData = function() {
    $.ajax({    //create an ajax request to load_page.php
        type: "GET",
        url: "second.php",             
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
            $("#responsecontainer").html(response);
            setTimeout(loadData, 1000); 
        }

    });
};
 </script>

 <div style='display:inline' id='responsecontainer'></div><!--only this one is displayed-->
 <div style='display:inline' id='responsecontainer'></div><!-- I want it to show me the same data here too-->

1 个答案:

答案 0 :(得分:2)

元素的ID在文档中必须是唯一的,使用class属性对相似的元素进行分组

<div style='display:inline' class='responsecontainer'></div>
<div style='display:inline' class='responsecontainer'></div>

然后使用类选择器

$(document).ready(function () {
    loadData();
});

var loadData = function () {
    $.ajax({ //create an ajax request to load_page.php
        type: "GET",
        url: "second.php",
        dataType: "html", //expect html to be returned                
        success: function (response) {
            $(".responsecontainer").html(response);
            setTimeout(loadData, 1000);
        }

    });
};

ID选择器将仅返回具有给定ID

的第一个元素