【问题标题】:My Json array only returns the last array. How to fix that?我的 Json 数组只返回最后一个数组。如何解决?
【发布时间】:2019-12-28 22:36:23
【问题描述】:

我的 JSON 数组有问题。这是我的 Ajax 代码(我上传文件 excel 并将其返回为 JSON。然后将 Json 转换为字符串并保存到 myDb):

function exportExcelToTable() {
    $('#upload-excel-convert').change(function (e) {
        let file = $(this).prop('files')[0];
        let formData = new FormData();
        formData.append("file",file);
        $("#js-render-btn-see-excel").html('');
        $('#js-table-excel tbody').html('');
        $.ajax({
            method:'POST',
            url: '/order/excel/read-file',
            data: formData,
            contentType: false,
            processData: false,
            dataType:'json',
            success(data) {
                let $data = $('input[name="order[sizeFile]"]');

                for (let i = 1; i < data.length; i++) {
                    $data.val(JSON.stringify(data[i]));
                    console.log(data[i]);
                }
            }
        });
    })
}

这是要返回的 Json console.log(data[i])

[["XS ", "Nam", null, null, null, 20],
 ["S", null, 13, null, null, 35],
 ["M", null, 12, null, null, 12]
]

而我的输入值只是返回最后一个数组

<input type="hidden" name="order[sizeFile]" value="["M";,null,12,null,null,12]">

我想要这样的输入值:

<input type="hidden" name="order[sizeFile]" value="[["XS ", "Nam", null, null, null, 20],["S", null, 13, null, null, 35],["M", null, 12, null, null, 12]]">

我该如何解决这个问题?谢谢。

P/s对不起我的英语。

UPDATE 我正在更新我的代码。

function exportExcelToTable() {
    $('#upload-excel-convert').change(function (e) {
        let file = $(this).prop('files')[0];
        let formData = new FormData();
        formData.append("file",file);
        $("#js-render-btn-see-excel").html('');
        $('#js-table-excel tbody').html('');
        $.ajax({
            method:'POST',
            url: '/order/excel/read-file',
            data: formData,
            contentType: false,
            processData: false,
            dataType:'json',
            success(data) {
                let $data = $('input[name="order[sizeFile]"]');
                let list;

                for (let i = 1; i < data.length; i++) {
                    list += JSON.stringify(data[i]);
                    $data.val(list);
                    console.log(data[i]);
                }
            }
        });
    })
}

没错,但我的输入显示 undefined 的值。

<input type="hidden" name="order[sizeFile]" value="undefined[["XS ", "Nam", null, null, null, 20],["S", null, 13, null, null, 35],["M", null, 12, null, null, 12]]">

【问题讨论】:

  • 这是你想要的吗?
  • JS 数组是从 0 开始的。
  • @sid 是的。这就是我想要的。 :))
  • 我更改了let i = 0。但它直到相同。 @DaveNewton
  • 尝试构建你的字符串然后设置val,而不是每次通过循环设置它。

标签: javascript jquery arrays json


【解决方案1】:

将控制台的输出存储在 var 中,然后在成功回调中,您可以使用该数组并替换输入上的值

你的 JS 应该是

success(data) {
    let $data = $('input[name="order[sizeFile]"]');


    var excelData = [];
    for (let i = 1; i < data.length; i++) {
        $data.val(JSON.stringify(data[i]));
        console.log(data[i]);
        excelData.push(data[i])
    }

    $("#showValues").val(excelData);

}

并为输入字段提供一个 id 以便以后能够替换该值

<input type="hidden" id="showValues" name="order[sizeFile]" value="">

【讨论】:

  • 是的。其作品。但是如何将我的输入与[["XS ", "Nam", null, null, null, 20],["S", null, 13, null, null, 35],["M", null, 12, null, null, 12]] 完全一样? :))
  • excelData 怎么说?
  • 我将您的代码更改为excelData.push(JSON.stringify(data[i]))。给我["XS ", "Nam", null, null, null, 20],["S", null, 13, null, null, 35],["M", null, 12, null, null, 12]
  • 我做到了。我只是添加'['+excelData+']'。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 2015-09-16
  • 1970-01-01
  • 2018-02-05
  • 2021-09-20
相关资源
最近更新 更多