<script type="text/javascript">
function LoadData(){
var url = serverURL+"/List.php";
$.ajax({
type: "GET",
url: url,
dataType: "json",
success: function(data){
$.each(data.data, function (key, value) {
var doc_id=value['id'];
var doc_name=value['name'];
var doc_speci = value['specialize'];
$("#myTable").append("<tr><td>"+doc_name+"</td><td>"+doc_speci+"</td><td class='retrieve' data-did="+doc_id+" style='cursor: pointer;'>EDIT</td></tr>");
});
},
error: function(data){
toastr.error("Opps! Something went wrong");
$(".se-pre-con").fadeOut("slow");
},
});
}
</script>
上面的内容将 tr 添加到下面的 html 表中。 HTML 表格如下:
<table id="myTable" class='table table-bordered table-hover table-striped'>
<tr>
<th>Name</th>
<th>Specialization</th>
<th>Action</th>
</tr>
</table>
现在我想从 td 类检索的数据属性中检索 id,以便我可以发送 id 并将其重定向到其他页面进行编辑。
最佳答案
// Clicks the edit field
$("td.retrieve").on("click", function(e) {
var id = $(e.currentTarget).attr("data-did");
// do with the ID what you want
alert(id);
});
我知道 jQuery 有一个“data(...)”函数,但我过去有时遇到过这个问题。 “attr(...)”将执行类似的操作,但具体依赖于属性而不是存储的对象。
关于javascript - 从表的td获取id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43941685/