Javascript 关于我在这个项目上到底做了什么,我非常乐意提供它 嗯。。输入元素的id为person1so。。使用它。谢谢你的反馈@GottZ!我尝试在tableCell上使用.getElementById(),但它会抛出一个错误,说“getElementById不是函数
Javascript 关于我在这个项目上到底做了什么,我非常乐意提供它 嗯。。输入元素的id为person1so。。使用它。谢谢你的反馈@GottZ!我尝试在tableCell上使用.getElementById(),但它会抛出一个错误,说“getElementById不是函数,javascript,html,csv,dom,nodelist,Javascript,Html,Csv,Dom,Nodelist,关于我在这个项目上到底做了什么,我非常乐意提供它 嗯。。输入元素的id为person1so。。使用它。谢谢你的反馈@GottZ!我尝试在tableCell上使用.getElementById(),但它会抛出一个错误,说“getElementById不是函数”。。。除此之外,我在问题中没有提到我的应用程序有很多和元素,所以我需要在一次调用中获取所有这些元素。其次,您正在谈论的.getElementById()调用在哪里。我在您提供的代码中看不到它。不要以为人们会偷走你在这里发布的所有东西。tbh我
关于我在这个项目上到底做了什么,我非常乐意提供它 嗯。。输入元素的id为
person1
so。。使用它。谢谢你的反馈@GottZ!我尝试在tableCell上使用.getElementById(),但它会抛出一个错误,说“getElementById不是函数”。。。除此之外,我在问题中没有提到我的应用程序有很多和元素,所以我需要在一次调用中获取所有这些元素。其次,您正在谈论的.getElementById()
调用在哪里。我在您提供的代码中看不到它。不要以为人们会偷走你在这里发布的所有东西。tbh我是那种甚至可以用更干净的代码重新实现你想法的人。ps:如果你遇到一个客户想要一个带有某种反作弊系统的表单,就用这个作为灵感吧:哦,我没有忽略它,因为我以为有人会偷。就像我在底部的帖子中所说的,我不能相信大部分代码,因为我正在跟随另一个开发人员的教程。我意识到我把它换成了编辑器中的代码来尝试,结果发现它不起作用。但我没有在这里反映这一点。再次感谢您的反馈!好。。输入元素的id为person1
so。。使用它。谢谢你的反馈@GottZ!我尝试在tableCell上使用.getElementById(),但它会抛出一个错误,说“getElementById不是函数”。。。除此之外,我在问题中没有提到我的应用程序有很多和元素,所以我需要在一次调用中获取所有这些元素。其次,您正在谈论的.getElementById()
调用在哪里。我在您提供的代码中看不到它。不要以为人们会偷走你在这里发布的所有东西。tbh我是那种甚至可以用更干净的代码重新实现你想法的人。ps:如果你遇到一个客户想要一个带有某种反作弊系统的表单,就用这个作为灵感吧:哦,我没有忽略它,因为我以为有人会偷。就像我在底部的帖子中所说的,我不能相信大部分代码,因为我正在跟随另一个开发人员的教程。我意识到我把它换成了编辑器中的代码来尝试,结果发现它不起作用。但我没有在这里反映这一点。再次感谢您的反馈!
<tr>
<td><input type="text" id="person1" name="person1"></td>
<td>
<select name="person1_lang1_primary" id="person1_lang1_select">
<option value=""></option>
<option value="english">English</option>
<option value="Spanish">Spanish</option>
<option value="other1">Other1 - specify below</option>
<option value="other2">Other2 - specify below</option>
<option value="other3">Other3 - specify below</option>
<option value="other4">Other4 - specify below</option>
</select>
</td>
<tr>
class TableCSVExporter {
constructor (table, includeHeaders = true) {
this.table = table;
this.rows = Array.from(table.querySelectorAll("tr"));
if(!includeHeaders && this.rows[0].querySelectorAll("th").length) {
this.rows.shift();
}
}
convertToCSV() {
const lines = [];
const numCols = this._findLongestRowLength();
for(const row of this.rows) {
let line = "";
for(let i = 0; i < numCols; i++) {
if (row.children[i] !== undefined) {
line += TableCSVExporter.parseCell(row.children[i]);
}
line += (i !== (numCols - 1)) ? "," : "";
}
lines.push(line);
// console.log(row);
}
return lines.join("\n");
}
_findLongestRowLength() {
return this.rows.reduce((l, row) => row.childElementCount > l ? row.childElementCount : l, 0);
}
static parseCell (tableCell) {
let input_elements = tableCell.getElementsByTagName("input");
let select_elements = tableCell.getElementsByTagName("select");
let inputArray = Array.from(input_elements);
let selectArray = Array.from(select_elements);
let elementsArray = [...inputArray, ...selectArray];
let parsedValue = [...elementsArray];
// Replace all double quotes with two double quotes
parsedValue = parsedValue.replace(/"/g, `""`);
// If value contains comma, new-line or double-quote, enclose in double quotes
parsedValue = /[", \n]/.test(parsedValue) ? `"${parsedValue}"` : parsedValue;
inputArray.forEach(element => console.log(element.value));
selectArray.forEach(element => console.log(element.value));
elementsArray.forEach(element => console.log(element.value));
return parsedValue;
}
<script>
const primaryInputByPerson = document.getElementById("primaryInputByPerson");
const btnExportCSV = document.getElementById("btnExportToCSV");
btnExportCSV.addEventListener("click", () => {
const exporter = new TableCSVExporter(primaryInputByPerson);
const csvOutput = exporter.convertToCSV();
const csvBlob = new Blob([csvOutput], {type: "text/csv" });
const blobURL = URL.createObjectURL(csvBlob);
const anchorElement = document.createElement("a");
anchorElement.href = blobURL;
// sets the name of the file the user will download
anchorElement.download = "LEAT-data.csv";
anchorElement.click();
// reduces the amount of memory used by the browser
setTimeout(() => {
URL.revokeObjectURL(blobURL);
}, 500);
})
// console.log(new TableCSVExporter(primaryInputByPerson).convertToCSV());
</script>