각 테이블 행과 해당 입력 (확인란, 텍스트, 선택)을 localstorage에 저장할 배열로 가져 오는 방법은 무엇입니까? 대부분의 코드를 완성했습니다. 빌드해야 할 부분은 ???? 부품. 감사.javascript 여러 배열을 json으로
이 자바 스크립트 코드입니다 : 당신은 내가 동적 테이블 행을 생성하는 방법을 궁금해하는 경우
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}
$('#btnSave').on('click', function(e) {
e.preventDefault();
var id = $(this).attr("data-id");
var mykey = 'set'+id;
// here just to check whether 'set'+id exist or not, no validation performed
if(localStorage.getItem(mykey)==null){
console.log('key not found');
} else {
console.log('key found');
}
// Due to there will be one or more rows,
// need to loop the rows and get the inputs within
// and store them in localstorage
$('#dyn_table tr').each(function(){
var tr = $(this);
var checkbox = $(this).find('.big-checkbox').is(':checked');
var itemcode = $(this).find('.dyn_item_code :selected').val();
var amount = $(this).find('.dyn_amount').val();
var reference = $(this).find('.dyn_reference').val();
console.log(checkbox);
console.log(itemcode);
console.log(amount);
console.log(reference);
});
localStorage.setItem(mykey, ????);
});
이 입력 버튼입니다
<input type="button" value="Add row" onClick="addRow('dyn_table')" />
네 말이 맞아요. 한 가지 더 질문합니다. 배열에 액세스하는 방법은 무엇입니까? 'console.log (localStorage.getItem (mykey) [0]);'? –
'JSON.parse()'를 사용하여 localStrorage에 저장된 문자열 (배열 stringify)을 배열로 변환합니다. 자세한 내용을 보려면 이전 답변을 확인하십시오. https://stackoverflow.com/questions/22991871/localstorage-save-array/22992002#22992002 – Arthur