我有一個表如下面的數據表表:追加Jquery的Ajax「的數據」作爲表中獨立的值
<button id="addRow">Add New Row</button><br>
<table class="table table-striped table-bordered table-hover " id="example" cellSpacing=0 width="100%">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
</thead>
<tr style="text-align: center;">
<td>hola</td>
<td>ciao</td>
<td>bonjour</td>
<td>yo</td>
<td>salut</td>
</tr>
</table>
我想使用JavaScript腳本追加元素如下所示:
<script type="text/javascript">
$(document).ready(function() {
debugger;
var t = $('#example').DataTable({ "searching": true, "paging": true });
var counter = 1;
$('#addRow').on('click', function ciicici() {
var now = new Date();
var now = now.toMysqlFormat();
var tii = new Date();
tii.setSeconds(tii.getSeconds() - 50000);
var tii = tii.toMysqlFormat();
$.post("sqlmachine_test_ajax.php", { timing: now,seconding: tii })
.done(function(data) {
t.row.add([
counter +'.1',
counter +'.2',
counter +'.3',
counter +'.4',
counter +'.5'
]).draw();
counter++;
// });
//setTimeout(function(){ciicici();}, 5000);
}); // Automatically add a first row of data
$('#addRow').click();
});
</script>
這兩個工作正常,唯一的薄弱之處是我想要通過Jquery AJAX腳本來追加元素。
比方說,我有一個PHP頁面發回的5個值我想補充到每一列(而不是C1的,C2的等等),如下所示:
<?php
echo 'counter1, counter2, counter3, counter4, counter5';
?>
,並在JavaScript的我想簡單地說:
...
.done(function(data) {
t.row.add([
data //(instead of the counters)
]).draw();
counter++;
...
我曾經嘗試這樣做,以及數組和JSON編碼的數組,但我得到的是5個結果表中的相同第一個單元格。 那麼我怎樣才能將ajax php響應追加到表中作爲表中不同單元格的數據呢?
馬爾科
你可能要編輯在受到注意,您使用的數據表,這是從簡單的追加到HTML表不同。 – Saeven
請添加JQuery標籤。 Javascript不是JQuery。 JQuery是我不想觸摸或調試的一些lib。 –
謝謝我已經添加了數據表和jquery –