如何将表行数据(HTML)从一个php页面移动到另一个php页面

时间:2018-01-04 04:18:57

标签: javascript html

 // searching the country to select(js file)
function search_country(){
  d3.csv("data/totalpopulation.csv", function(data) {
  var v=f1.search.value; 
    for(var i=0;i<data.length;i++){
      if(data[i].Name==v){
        document.getElementById('tablediv').style.visibility="visible";
        // retrun value
        document.getElementById('poptable').innerHTML+="<tr id='"+data[i].Name+"' class='ComparisonTable'  onclick='current_country(this.id)' style='cursor:pointer'><td>" + data[i].Name + "</td><td>" + (data[i].p1961/1000000).toFixed(2) + "</td><td>" + (data[i].p2016/1000000).toFixed(2) + "</td></tr>";
    }
  }
});
}

// Comparing the Selected Countries
function compare(){
window.location.href='country.php';
}

在这里,我从搜索栏中选择国家并放入CompareTable。 Furthur,在点击比较按钮时,我应该移动到下一个php文件(country.php)并携带相同的表格数据进行比较。

 document.getElementById('poptable').innerHTML+="<tr id='"+data[i].Name+"' class='ComparisonTable'  onclick='current_country(this.id)' style='cursor:pointer'><td>" + data[i].Name + "</td><td>" + (data[i].p1961/1000000).toFixed(2) + "</td><td>" + (data[i].p2016/1000000).toFixed(2) + "</td></tr>";

1 个答案:

答案 0 :(得分:0)

使用arrya存储完整的tr值

function getClosestValue(standardArray, targetVal) {
standardArray = standardArray.sort(function(a, b){return a-b});
        if (!(standardArray) || standardArray.length == 0) {
            return null;}
        if (standardArray.length == 1){
            return standardArray[0];}

        for (var i=1; i<standardArray.length; i++) {
            if (standardArray[i] > targetVal) {
                var prevs = standardArray[i-1];
                var currnt = standardArray[i]
                return Math.abs( prevs-targetVal ) < Math.abs( currnt-targetVal ) ? prev : currnt;
            }
        }
        return standardArray[standardArray.length-1];        
    }

var temp = getClosestValue([110,111,188,190,177,120,140,120,149,148,155], 149)
console.log(temp);

如果要在var data_arr[]; function search_country(){ d3.csv("data/totalpopulation.csv", function(data) { var v=f1.search.value; for(var i=0;i<data.length;i++){ if(data[i].Name==v){ document.getElementById('tablediv').style.visibility="visible"; // push value to an object. data_arr.push(document.getElementById('poptable').innerHTML+="<tr id='"+data[i].Name+"' class='ComparisonTable' onclick='current_country(this.id)' style='cursor:pointer'><td>" + data[i].Name + "</td><td>" + (data[i].p1961/1000000).toFixed(2) + "</td><td>" + (data[i].p2016/1000000).toFixed(2) + "</td></tr>"); } } }); } // Comparing the Selected Countries function compare(){ data_arr_json = JSON.stringify(data_arr); window.location.href='country.php?data='+data_arr_json; } 中获取数组数据:

country.php