动态添加整数值到表

时间:2011-03-17 17:30:54

标签: javascript jquery html-table

如果我必须添加一个整数值,例如newFeature.OrderTotal中有一个整数值。

如何在动态创建的表格中动态显示它?

任何建议都会受到赞赏。

txt += "<td>"+newFeature.OrderTotal+"</td>"; 

如何做到这一点?

 function dataSelectHandler(transaction, results){

  // Handle the results
  for (var i=0; i<results.rows.length; i++) {

    var row = results.rows.item(i);

    var newFeature = new Object();

        newFeature.OrderTotal   = row['OrderTotal'];//OrderTotal is coming from `(//"SELECT SUM(amount1) AS OrderTotal FROM budget1 WHERE month='"+hello+"' ";)`
    var q1 = newFeature.OrderTotal;//q1 has an integer value suppose 46
    alert(q1);//here alert box is showing properly value of q1
    }
}

var txt = "";
var myTable = "";
txt += "<table id='myTable' border='5'>";
txt += "<tr>";
txt += "<th>Month</th>";
txt += "<th>Amount</th>";
txt += "</tr>";
txt += "<tr>";
txt += "<td>"+$('#mchoose').val()+"</td>";
txt += "<td>"+q1+"</td>";//m not getting any values in this q1 in the table row
txt += "</tr>";
txt += "</table>";

$('#ss').html(txt);

2 个答案:

答案 0 :(得分:0)

您需要创建一个包含行和包含元素的数据的表,这是一个示例:

function addTable(parentElementId) {
  var data = [1, 2, 3, 4];
  // Create the table with a row and cell for each data element.
  var t = document.createElement("table");
  t.id = "myTable";
  for (var i=0; i<data.length; i++) {
    var tr = t.appendChild(document.createElement("tr"))
      , td = tr.appendChild(document.createElement("td"));
    td.innerHTML = data[i];
  }
  // Add the table to the given element.
  var p = document.getElementById(parentElementId);
  p.appendChild(t);
}

答案 1 :(得分:0)

如果这个想法适合您的问题,那么您可以获得代码:

(1)直接在#ss html中添加表格,或者从jquery $(“#ss”)。html(tableText)中按照自己的方式添加。请注意,正确的代码层次结构为table > tbody > tr > td,因为trtd是动态的。所以只需添加tabletbody

(2)在dataSelectHandler() for循环中,以"<tr><td>"+newFeature.OrderTotal+"</td></tr>"格式构建字符串,并附加到$("#myTable > tbody").html(rowText);之类的tbody以替换alert();