【发布时间】:2019-05-17 08:18:25
【问题描述】:
我想将每一行的数据库表 ID 作为动态 html 表的复选框值
我正在使用 ajax 从 mysql 数据库中获取数据并创建一个新变量作为 html 文本附加到表的 tbody 上
HTML代码
<div class="col-sm-6" id="ftbl">
<label for="urbandata">View urban data</label>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Check</th>
<th>ID</th>
<th>Type</th>
<th>Master</th>
<th>Slave</th>
<th>Orbit</th>
<th>Mode</th>
<th>Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
JS代码
$.ajax({
url: "fetchurban.php",
method: "POST",
data:{id:id},
dataType: "JSON",
success: function(data){
if (data){
var trHTML = '';
$.each(data, function (i, item) {
trHTML +='<tr><td><input type="checkbox" id="checkview" onclick="QuickView()" name="'+ item.TblID +'"></td><td>' + item.Type + '</td><td>' + item.Master + '</td><td>' + item.Slave + '</td><td>' + item.Orbit + '</td><td>' + item.Mode + '</td><td><a href='+ item.ImgTIF+ ' title="Download High Quality" data-toggle="tooltip"><span class="glyphicon glyphicon-download"> </span></a><a href=#?id='+ item.ImgLow + ' title="Download Low Quality" data-toggle="tooltip"><span class="glyphicon glyphicon-cloud-download"></span></a></td></tr>' ;
});
$('#ftbl tbody').append(trHTML);
}
}
})
})
如果用户选择复选框,我希望获得数据库表的 ID。 现在有了这段代码,我对表的所有行都有相同的 id
【问题讨论】:
-
ID 在 HTML 文档中必须是唯一的,因此您不能在此处开始使用
id="checkview"。 // 你是指这里的name="'+ item.TblID +'"部分吗?那么可能你所有的项目都具有相同的TblID值。 -
你能分享一些你从服务器收到的 json 吗?正如@04FS 所说,您的 json 中可能有相同的 id
标签: javascript php html mysql ajax