![](/img/trans.png)
[英]How to update score dynamically using jquery and php without page refresh
[英]Add a <tr> element to dynamic table, dynamically without a page refresh, php jquery
我试图在单击按钮时向现有表中添加动态行,该行的行是使用PHP脚本的输出动态创建的。
我对脚本insert_tr.php
进行了ajax调用,该脚本以与现有数据表相同的格式返回了TR元素给我,并适当地返回了数据
但是不幸的是, <tr>
行不是动态添加到表中,而是仅在页面刷新后才添加。
PHP文件代码:
<div id="v_div">
<table class="table table-bordered table-striped" >
<thead>
<th class='col-md-2'>name</th>
<th class='col-md-2'>number</th>
</thead>
<tbody>
<?php
while ($data = pg_fetch_assoc($ret)) {
echo
"<tr id=tr_".$data['sr_number'].">
<td class='td_topic' id=title:".$data['number']." >".trim($data['name'])."</td>
<td class='td_topic' id=title:".$data['number']." >".trim($data['number'])."</td>
<td class='td_topic' id=title:".$data['number']." ><button class='btn btn-info check1' type=button title='Add Entry'>Add Entry</button></td>
</tr>";
?>
</tbody>
</table>
</div>
Javascript:
$(document).ready(function() {
$("#v_div").on('click', '.check1', function() {
var field_userid = $(this).parent().attr("id");
var value = $(this).text();
$.post('insert_tr.php', field_userid + "=" + value, function(data) {
if (data != '') {
$(this).closest("tr").after(data);
}
});
});
});
我要做的就是在当前TR
启用后立即添加行,动态地不刷新页面,这最终可以使用ajax调用。
到参考this
是不被点击的按钮。
$(this).closest("tr").after(data);
存储对Ajax调用之外的行的引用。
$(document).ready(function() {
$("#v_div").on('click', '.check1', function() {
var field_userid = $(this).parent().attr("id");
var value = $(this).text();
var row = $(this).closest("tr");
$.post('insert_tr.php', field_userid + "=" + value, function(data) {
if (data != '') {
row.after(data);
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.