javascript - 从 HTML 表中提取数据以将值存储在变量中
问题描述
我想知道如何将数据库中的值放入javascript中的变量中。我不是网络开发人员,所以我在 HTML/CSS/Javascript 方面没有那么丰富的经验,因此不胜感激。我的代码如下:
$(document).ready(function() {
$("#display").click(function() {
$.ajax({ //create an ajax request to display.php
type: "GET",
url: "GetOccupancy.php",
dataType: "html", //expect html to be returned
success: function(response) {
$("#responsecontainer").html(response);
//alert(response);
}
});
});
});
function GetCellValues() {
var table = document.getElementById('#responsecontainer');
for (var r = 0, n = table.rows.length; r < n; r++) {
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
alert(table.rows[r].cells[c].innerHTML);
}
}
}
解决方案
好吧,一种方法是设置一个二维数组(矩阵),然后将值推入:
function GetCellValues() {
var table = document.getElementById('#responsecontainer');
var tableMatrix = []; //Set up an empty array (put it outside the function if you want to use it again somewhere else)
for (var r = 0, n = table.rows.length; r < n; r++) {
var rowValues = []; //Create an array to hold all the values in this row
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
rowValues.push( table.rows[r].cells[c].innerHTML ); //Add the values to the row
}
tableMatrix.push(rowValues); //Now add the row to the table matrix
}
}
但是,这实际上取决于您希望如何访问它。事实上,它可能按原样可用 - 即您可以复制table.rows
到一个新变量并以这种方式存储它。这一切都取决于你想用它做什么。
推荐阅读
- java - 两个属性的 xpath
- ios - UINavigationController 中的 UISegmentedControl:在 segue 上保持相同的位置
- c# - 为什么打开和关闭套接字会导致数据丢失?
- ios - 按大小类获取约束常量
- java - 将图表保存为 pdf 时消除模糊图表
- vba - 宏突出单元格中的变化,范围不断变化
- c# - 使用返回选定字段并在两个查询中排序的子查询查询 linq
- javascript - 使用 react-router 重定向服务器端 onClick
- azure-api-management - 导入开放 API 规范时 Azure API 管理不遵守 operationId
- regex - 使用 bash 脚本将 IANA 后向文件转换为 json 文件