我传递了$display
个变量:
echo json_encode(array('status' => TRUE, 'display'=>$display)); die;
jQuery和AJAX:
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data)
{
var res = $.parseJSON(data);
if(res.status == true)
{ var results='';
if(res.display[i].type=='1')
{
var b="Business";
}
if(res.display[i].type=='2')
{
var b="Economy";
}
for(var i=0; i<res.display.length; i++)
{
results +='<tr><td>'+(i+1)+'</td><td>'+res.display[i].name+'</td><td>'+b+'</td><td>'+res.display[i].seat+'</td><td>'+res.display[i].date_booked+'</td><td></td><td><a href="" class="remove" data-id='+res.display[i].id+'>Cancel ticket</a></td></tr>'
}
}
res.display[i].date_booked
,res.display[i].seat
,res.display[i].name
很好,但data-id='+res.display[i].id+'
出了什么问题?
答案 0 :(得分:0)
你忘记了引号:
// Note the `a` tag's `data-id` is changed from:
// data-id='+res.display[i].id+'>
// to
// data-id="'+res.display[i].id+'">
results += '<tr>' +
'<td>'+(i+1)+'</td>' +
'<td>'+res.display[i].name+'</td>' +
'<td>'+b+'</td>' +
'<td>'+res.display[i].seat+'</td>' +
'<td>'+res.display[i].date_booked+'</td>' +
'<td></td>' +
'<td><a href="" class="remove" data-id="'+res.display[i].id+'">Cancel ticket</a></td>' +
'</tr>';
我可以建议您按如下方式格式化代码吗?所以它更具可读性:
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
var res = $.parseJSON(data);
if (!res.status) return;
var results = '';
for (var i = 0; i < res.display.length; i++) {
results += '<tr>' +
'<td>'+(i+1)+'</td>' +
'<td>'+res.display[i].name+'</td>' +
'<td>'+b+'</td>' +
'<td>'+res.display[i].seat+'</td>' +
'<td>'+res.display[i].date_booked+'</td>' +
'<td></td>' +
'<td><a href="" class="remove" data-id="'+res.display[i].id+'">Cancel ticket</a></td>' +
'</tr>';
}
}
});