JS变量无法在AJAX中成功运行

时间:2018-08-29 16:32:57

标签: javascript ajax

使用php和ajax从数据库中获取数据,一切正常,除了note变量为空。有人可以找出为什么note值不显示的问题。我该如何解决。

success: function(response) {

var cats = {};
response.results.forEach(function(element) {
    cats[element.team] = cats[element.team] || [];
    cats[element.team].push(element);
});

var i = 0;

Object.keys(cats).forEach(function(team) {
    let html = '';

    // Append the category header
    html = '<tr>';
    html += '<td>' + team + '</td>';
    html += '</tr>';
    $('table').append(html);

    // Loop through the results for this category
    cats[team].forEach(function(element) {
        var id = element.id;
        var teamId = element.team_id;
        var names = element.player;
        var result = element.result;
        var note = element.note;

        html = '<tr>';
        html += '<input type="hidden" name="Id[]" value="' + id + '"/>';
        html += '<input type="hidden" name="data[' + i + '][team_id]" value="' + teamId + '"/>';
        html += '<td>' + names + '</td>';
        html += '<td><input type="text" name="result[]" value="' + result + '"></td>';
        html += '</tr>';
        $('table').append(html);
    });

    // Append the textarea at the end of the results
    html = '<tr>';
    html += '<td><textarea placeholder="note..." name="data[' + i + '][Note]">' + note + '</textarea></td>';
    html += '</tr>';
    $('table').append(html);
    i++;
});

}

这是输出

enter image description here

数据库表

enter image description here

JSON输出

{"groupData":{"id":"23","group_name":"Group B"},"results": 
[{"id":"2","team_id":"4","team":"Team 
B","player":"Deno","result":"14","note":"Lost"}, 
{"id":"3","team_id":"4","team":"Team 
B","player":"Niob","result":"26","note":"Lost"}, 
{"id":"4","team_id":"4","team":"Team 
B","player":"Skion","result":"76","note":"lost"}, 
{"id":"5","team_id":"5","team":"Team 
C","player":"Bela","result":"47","note":"won"}, 
{"id":"6","team_id":"5","team":"Team 
C","player":"yomi","result":"57","note":"won"}]}

1 个答案:

答案 0 :(得分:1)

您应该在cats [team] .forEach(function(element){})前面设置音符值。希望对您有帮助,我的朋友:))

    success: function(response) {

var cats = {};
response.results.forEach(function(element) {
    cats[element.team] = cats[element.team] || [];
    cats[element.team].push(element);
});

var i = 0;

Object.keys(cats).forEach(function(team) {
    let html = '';

    // Append the category header
    html = '<tr>';
    html += '<td>' + team + '</td>';
    html += '</tr>';
    $('table').append(html);

    var note;
    // Loop through the results for this category
    cats[team].forEach(function(element) {
        var id = element.id;
        var teamId = element.team_id;
        var names = element.player;
        var result = element.result;
        note = element.note;

        html = '<tr>';
        html += '<input type="hidden" name="Id[]" value="' + id + '"/>';
        html += '<input type="hidden" name="data[' + i + '][team_id]" value="' + teamId + '"/>';
        html += '<td>' + names + '</td>';
        html += '<td><input type="text" name="result[]" value="' + result + '"></td>';
        html += '</tr>';
        $('table').append(html);
    });

    // Append the textarea at the end of the results
    html = '<tr>';
    html += '<td><textarea placeholder="note..." name="data[' + i + '][Note]">' + note + '</textarea></td>';
    html += '</tr>';
    $('table').append(html);
    i++;
});

}