将内容添加到div

时间:2016-02-07 21:31:43

标签: javascript jquery html css

我目前正在使用此代码从按钮点击页面获取内容:

function getfilter(str){
  $.ajax({
     type: "GET",
        //path to  php page to get  data
    url:"getreceipt.php?table="+str+"",
    success:function(html){

      //here is your success action
      //get data on div  
        $("#receipt").html(html);
    }
    });
 }

然后,我想使用此代码将innerHTML内容添加到上一代码创建的内容之上的同一个div中:

function updateCarList(transaction, results) {
    //initialise the listitems variable
    var listitems = "";
    //get the car list holder ul
    var listholder = document.getElementById("receipt");

    //clear cars list ul
    listholder.innerHTML = "Receipt<br>";

    var i;
    //Iterate through the results
    for (i = 0; i < results.rows.length; i++) {
        //Get the current row
        var row = results.rows.item(i);

        listholder.innerHTML += "Chair "+row.chair+"-<input type='hidden' id='chair[]' name='chair[]' value='"+row.chair+"' /><input type='hidden' id='item' name='item[]' value='"+row.item1+"' /><input type='hidden' id='price' name='price[]' value='"+row.price+"' />" + row.item1 + " - $" + row.price + " (<a href='javascript:void(0);' onclick='deleteCar(" + row.id + ");'>Delete Car</a>)<br>";
    }

}

我尝试了多种方式,包括appendTo,append,text,html,但似乎没有任何效果。它现在的方式只是替换了从getreceipt.php

获取的代码

任何人都可以提供任何建议吗?谢谢!

1 个答案:

答案 0 :(得分:0)

&#34;附加&#34;应该工作,如果你已经在使用JQuery,则使用innerHTML退出。我实际上会创建一个单独的div并将所有行附加到此div中,然后将此div附加到列表持有者中。

... // your code above
//Iterate through the results
var chairs = $('<div></div>');
for (i = 0; i < results.rows.length; i++) {
    var row = results.rows.item(i);
    var rowHTML = "<p>Chair "+row.chair+"-<input type='hidden' id='chair[]' name='chair[]' value='"+row.chair+"' /><input type='hidden' id='item' name='item[]' value='"+row.item1+"' /><input type='hidden' id='price' name='price[]' value='"+row.price+"' />" + row.item1 + " - $" + row.price + " (<a href='javascript:void(0);' onclick='deleteCar(" + row.id + ");'>Delete Car</a>)</p>";
    chairs.append(rowHTML); 
}
listholder.append(chairs);
... // your code - actually only function closing.