Comment obtenir un index de colonnes et de lignes correct?

Étant donné une table comme:

Comment puis-je sauvegarder la valeur d’ th input la colonne avec ses row input data ?

J’ai essayé de prouver un cours, mais ça ne me mène nulle part:

 $('#saveTable').on("click", function() { var myTableArray = $("table th input").val(); var $this = $(this); var myIndex = 0; $("table th input").each(function() { $(this).attr("data-index", "_" + myIndex); var thisLabelData = $(this).attr("data-index"); $("table tbody input").each(function() { console.log(thisLabelData); if(!$(this).hasClass(thisLabelData)) { $(this).addClass(thisLabelData); } }); //$this.push(myTableArray); myIndex++; }); //console.log(myTableArray); }); 

terrain de jeu jsFiddle

vérifier cela et laissez-moi savoir

 $('#saveTable').on("click", function() { var results={}; $('thead input').each(function(){ results[$(this).val()] = []; }); var resultsKeys = Object.keys(results); $('tbody input').each(function(){ var colIndex = $(this).parent().index(); var rowIndex = $(this).closest('tr').index(); results[resultsKeys[colIndex-1]] .push("(" + (colIndex-1) + " " + rowIndex + ") -> " + ($(this).val() !== '' ? $(this).val() : "0")); }); console.log(results); }); $('#table-data input').on("change", function() { $(this).attr("value", $(this).attr("value")); }); $(".table-ssortingped tbody tr th input").each(function(){ $(this).addClass("column_data"); }); $("#addTr").on('click', function() { var $tr = $('tbody tr.tr_clone'); var $clone = $tr.clone(); $clone.find(':text').val(''); $tr.after($clone); $(".table-ssortingped tbody tr td input").each(function(){ $(this).addClass("row_data"); }); }); $("#addTd").on("click", function(){ $(".table-ssortingped thead tr").append('
'); $(".table-ssortingped tbody tr").append(' '); $(document).find("thead th input").each(function(){ $(this).addClass("column_data"); }); $(".table-ssortingped tbody tr td input").each(function(){ $(this).addClass("row_data"); }); }); $(document).on("click", ".removeRow", function(){ $(this).parent().parent() $(this).parent().parent().remove(); }); $(document).on("click", ".removeColumn", function(){ var index = $(this).parent().index() + 1; $(this).closest("table").find("td:nth-child(" + index + ")").remove(); $(this).parent().remove(); });
  

Insert your data