簡體   English   中英

使用ajax jquery將每個表行中的所有數據保存

[英]Save all data in each table row using ajax jquery

我如何使用ajax jquery將每個表行中的每個數據保存在我有此表的地方

在此處輸入圖片說明 我想將其保存為我的sql數據庫中的相同內容,或者至少獲取每個表行並將其循環到每行數,讓我說我將所有值保存在行引用ID 1中。那是什么? 我想將全部數據保存為與輸出相同的數據到目前為止,我在所有數據單元中都獲得了全部價值。

使用此代碼

$('table tr td').each(function(){
    arr.push($(this).text());                                                       
    });
    $.each(arr,function(index,value){
        alert(arr[index]);
    });  

我可以給你另一個解決方案。
1.將數據保存在數組中
2.將其轉換為JSON字符串。
3.使用NewtonSoft庫將JSON字符串轉換為DataSet。

現在,您可以將該數據集保存到數據庫中(使用存儲過程保存)

*優點是您可以一次調用保存整個表。

您可以使用這種方式:

<table id="testTable">
    <tr><td>Name</td><td>Class</td></tr>
    <tr><td>Test Name 1</td><td>Class 1</td></tr>
    <tr><td>Test Name 2</td><td>Class 2</td></tr>
    <tr><td>Test Name 3</td><td>Class 3</td></tr>
    <tr><td>Test Name 4</td><td>Class 4</td></tr>
</table>

<script>

    $(document).ready(function(){

        headings=[];
        tableRowData=[];        

        $('#testTable tr').eq(0).each(function(){

            $(this).find('td').each(function(){

                tdText=$(this).text();                  
                headings.push(tdText);
            });

        });

        tableRowData=[];            
        $.each(headings , function(i, val) { 
            tableRowData[i]=[]; 
        });

        $('#testTable tr').not(':first').each(function(){

            i=0;
            $(this).find('td').each(function(){

                tdText=$(this).text();

                tableRowData[i].push(tdText);
                i++;
            });

        });         

        console.log(headings);
        console.log(tableRowData);

        $.ajax({
            url: 'test.php',
            type: 'get',
            async: false,               
            data: {headings:headings,tableRowData:tableRowData,},
            success: function(response_msg){                        

                //Success                                       
            },
            error:function(){

                alert('Failure, some problem');

            }
        });

    });

</script>

這樣可以將表數據以Array的形式獲取到PHP服務器頁面中,可以使用兩個數組的鍵來完成標題和標題數據的映射。

暫無
暫無

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

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