Table with twitter bootstrap and jQuery

I have this js part of script:

jQuery.each(data, function(index, value) {
     $("table_div").append("<td>" + value + "</td>");
 });

I want use this for create a table with twitter bootstrap. In the html page there is this table element:

<table class="table table-striped" id="table_div">
</table>

But this solution doesn't works. How I have to do? Thank you!


首先,你不追加任何表中所需的<tr>元素,其次你指的是$("table_div")而不是$("#table_div")#标签表示你是指一个ID,就像在CSS中一样)。

jQuery.each(data, function(index, value) {
     $("#table_div").append("<tr><td>" + value + "</td></tr>");
});

Besides referring to the node <table_div> instead of the id #table_div you don't want to append anything to the table node.

You should take a look at this as well as here and here.

You should use tbody when using Twitters Bootstrap anyways for example, like so:

<table id="table_div" class="table table-striped">
  <tbody></tbody>
<table>

here the right js

for (i in data) {
  $('#table_div > tbody:last').append('<tr><td>'+data[i]+'</td></tr>');
}

For more research look here Add table row in jQuery

Edit:

Ok i wrote you an entire example using twitters bootstrap and jQuery. This works, if it doesn't for your data array, something is wrong with it.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
</head>
<body>
<table class="table table-striped" id="my-table">
<tbody>
</tbody>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.js"></script>
<script type="text/javascript">
var data = ["foo","bar"];
$(document).ready(function(){
        $.each(data, function(i,item){
                $('#my-table > tbody:last').append('<tr><td>'+item+'</td></tr>');
        });
});
</script>
</body>
</html>
链接地址: http://www.djcxy.com/p/22988.html

上一篇: 如何使用jQuery在html表中动态添加新行

下一篇: 表与twitter引导和jQuery