簡體   English   中英

我的Json數組僅返回最后一個數組。 如何解決?

[英]My Json array only returns the last array. How to fix that?

我的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]]">

將控制台的輸出存儲在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="">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM