簡體   English   中英

如何使用從 AJAX 響應收到的 JSON 數據根據 ID 更新表行

[英]How to update Table rows based on ID with JSON data received from AJAX response

我有一個 json 響應對象,我需要用來自 jquery 的 ids 和 td 更新表行,以便每當我調用 ajax 函數時,表的所有值都用 json 對象更新。 請幫幫我,我在這里添加代碼。 任何幫助將是可觀的.....問候

阿賈克斯:

$.ajax({
        type: 'POST',
        url: URI_PREFIX+'/getDetails',
        data: JSON.stringify({devId}),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
       }).done(function (data, textStatus, jqXHR) {

                  if(jqXHR.status==206){
                   alert("Invalid Username or Password");
                  }
                  else if(jqXHR.status==412){
                   alert("Your Acount is not activated");
                  }
                  else if(jqXHR.status==200){
                      var newData = data.masters;

                      $.each(newData, function(i, item) {

                          $('#branch2 td').
                            var $tr = $('<tr>').append(
                                $('<td>').text(item.rank),
                                $('<td>').text(item.content),
                                $('<td>').text(item.UID)
                            ); //.appendTo('#records_table');
                            console.log($tr.wrap('<p>').html());
                        });


                  }

                 });

桌子 :

<table class="table table-bordered"
                                style="width: 100%; margin-top: 20px;" id="descTable">
                        <thead style="text-align: center;">
                            <tr>
                                <th class="color"></th>
                                <th class="color">Amp</th>
                                <th class="color">Volt</th>
                                <th class="color">PF</th>
                                <th class="color">Watt</th>
                                <th class="color">Kwh</th>
                                <th class="color">Lrh</th>
                                <th class="color">Temp1</th>
                                <th class="color">Temp2</th>
                            </tr>
                                </thead>
<tbody style="text-align: center;" class="omega">

                            <tr class="color1" id="branch1">
                                <td>Branch1 Real Values</td>
                                <td class="optAmp" style="font-family: 'calc'">ajax value</td>
                                <td class="optVolt" style="font-family: 'calc'">ajax value</td>
                                <td class="optPf" style="font-family: 'calc'">ajax value</td>
                                <td class="optWatt" style="font-family: 'calc'">ajax value</td>
                                <td class="optKwh" style="font-family: 'calc'">ajax value</td>
                                <td class="optLhr" style="font-family: 'calc'">ajax value</td>
                                <td class="optTmp1" style="font-family: 'calc'">ajax value</td>
                                <td class="optTmp2" style="font-family: 'calc'">ajax value</td>
                            </tr>
</tbody>
</table>

要訪問 tr 中class="optAmp"的 td(例如, id="brach1"id="brach1" ),您可以使用$('tr#branch1 td.optAmp')

因此,要更改它的值,您可以在 each 函數中執行以下操作:

$('tr#branch1 td.optAmp').text(item.optAmp); // I assume your item object has that property
$('tr#branch1 td.rank').text(item.rank);
// and on ...

暫無
暫無

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

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