[英]jQuery giving undefined value in modal
I want to show data from a database to table that is in modal, i already try this code. 我想将数据从数据库显示为模态表,我已经尝试过此代码。 But, it keep giving undefined value 但是,它不断提供不确定的价值
here's the image: 这是图像:
Modal code: 模态代码:
<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<form id="myForm" action="" method="post" class="form-horizontal">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>NIK</th>
<th>Nama</th>
</tr>
</thead>
<tbody id="TampilDataModal">
</tbody>
</table>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Script : 剧本:
$('#TampilData').on('click','.item-detail-plp', function(){
var idPelapor = $(this).attr('data');
$('#myModal').modal('show');
$.ajax({
url: 'GetPelapor',
async: false,
method: 'get',
type: 'ajax',
data: {idPelapor:idPelapor},
datatype: 'json',
success: function (data) {
var html = '';
var i;
for(i=0; i<data.length; i++){
html +='<tr>'+
'<td>'+data[i].idPelapor+'</td>'+
'<td>'+data[i].nama+'</td>'+
'</tr>';
}
$('#TampilDataModal').html(html);
}
});
});
response from json [{"idPelapor":"3","nama":"a"}]
json [{"idPelapor":"3","nama":"a"}]
响应
You need to convert your JSON into an object literal, otherwise it will loop through each character of the JSON string and try to get a property of each character which will be undefined. 您需要将JSON转换为对象文字,否则它将遍历JSON字符串的每个字符并尝试获取每个字符的未定义属性。
function (data) {
data = JSON.parse(data);`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.