簡體   English   中英

遍歷數組並打印html foreach鍵/值的Javascript / jQuery

[英]iterate through array and print html foreach key/value Javascript / jQuery

好的,因此此功能可以完成我期望的所有操作,直到我點擊“打印到html”部分為止。 無論出於何種原因,它只會一次打印出一個索引,而不會打印數組中的每個索引。

該代碼的違規部分位於所包含的代碼段中的第19-23行。

$.each(_services, function(index,value)
{
    $('#test').html('<p>' +index+ ":" +value+ '</p>');
});

 // services object -- initially EMPTY var _services = {}; $('#serviceList input[type=checkbox]').change( function serviceList() { // For each checkbox inside the #serviceList container, do the following $(this).each( function() { // Set "_key" to id of checkbox var _key = (this).id; // If checkbox is checked -- ADD -- its key/value pair to the "services" object // else checkbox is unchecked -- DELETE -- key/value pair from "services" object (this.checked) ? _services[ _key ] = (this).value : delete _services[ _key ]; // console.log(_services); }); // END foreach() // foreach services print paragraph to display key/value $.each(_services, function(index,value) { $('#test').html('<p>' +index+ ":" +value+ '</p>'); }); // END services foreach }); // END serviceList() 
 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> </head <body> <ul id="serviceList"> <!-- changed to list in the tutorial --> <li><input id="id_a" type="checkbox" value="40"> Test A</li> <li><input id="id_b" type="checkbox" value="50"> Test B</li> <li><input id="id_c" type="checkbox" value="60"> Test C</li> <li><input id="id_d" type="checkbox" value="70"> Test D</li> </ul> <div id="test" style="height: 500px; width: 700px; background-color: pink;"></div> </body> 

每次編寫新行時,您都在清除容器的innerHTML 相反,您應該在each循環之前附加內容並清除容器:

$('#test').empty(); 
$.each(_services, function(index,value) {
    $('#test').append('<p>' +index+ ":" +value+ '</p>');
});

查看下面的演示:

 var _services = {}; $('#serviceList input[type=checkbox]').change(function serviceList() { // For each checkbox inside the #serviceList container, do the following $(this).each(function () { // If checkbox is checked -- ADD -- its key/value pair to the "services" object // else checkbox is unchecked -- DELETE -- key/value pair from "services" object this.checked ? _services[this.id] = this.value : delete _services[this.id]; // console.log(_services); }); // END foreach() // foreach services print paragraph to display key/value $('#test').empty(); $.each(_services, function (index, value) { $('#test').append('<p>' + index + ":" + value + '</p>'); }); }); // END serviceList() 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul id="serviceList"> <!-- changed to list in the tutorial --> <li><input id="id_a" type="checkbox" value="40"> Test A</li> <li><input id="id_b" type="checkbox" value="50"> Test B</li> <li><input id="id_c" type="checkbox" value="60"> Test C</li> <li><input id="id_d" type="checkbox" value="70"> Test D</li> </ul> <div id="test" style="height: 150px; background-color: pink;"></div> 

如果正確理解您的問題,那是因為您每次都覆蓋#test元素的內容。 如何追加:

   $('#test').append('<p>' +index+ ":" +value+ '</p>');

暫無
暫無

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

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