gpt4 book ai didi

javascript - 我必须将数组数据打印到表中

转载 作者:行者123 更新时间:2023-11-28 00:25:06 25 4
gpt4 key购买 nike

我必须将用户定义的数组数据打印到表中。一旦用户点击添加按钮。我能够从表中打印数据。但是我想把它打印在表格里面。

这是我的代码:

var data = new Array(); // blank array declaration

function check_info() {
var text = document.getElementById("enter").value;
var txtage = document.getElementById("age").value;

if (text == "" || txtage == "") {
console.log("Check Function load");
alert('Please fill the Field');
return false;
} else {
return display();
}
}

function display() {
var str1 = document.getElementById("enter").value;
var str2 = document.getElementById("age").value;

document.getElementById('enter').value = '';
document.getElementById('age').value = '';

if ((str1 == "" || str1.length == 0) && (str2 == "" || str2.length == 0)) {
return false;
}
data.push(str1);
data.push(str2);
document.getElementById("str1").children[0].innerHTML += "<li>" + data[data.length - 1] + "</li>";
document.getElementById("str2").children[1].innerHTML += "<li>" + data[data.length - 1] + "</li>"

}
<html>
<body>
<lable>Enter name : </lable> <input type="text" id="enter" required/>
<lable>Enter age : </lable> <input type="text" id="age" required/> <input type="button" value="Add" onclick="check_info()">
<div id='str'>
<ol></ol>
</div>
<table align='center' cellspacing=2 cellpadding=5 id="data_table" border=1>
<thead>
<tr>
<th> Name</th>
<th> Age</th>
<th> Action</th>
</tr>
</thead>
</table>
</body>

</html>

我想打印表格中的数据。谁能告诉我到底是什么问题?

最佳答案

您可以尝试这样做以保持代码干爽和干净,而无需使用任何重载库...您还应该考虑从表格中删除 cellspacing 和 cellpadding 属性,因为它们在 html5 中已被弃用,请改用 css 来设置表格样式。

var data = [];
function check_info()
{
// without the trim function a user can easily
// bypass this typing just whitespaces.
// do you really want the age to be any character?
var enter = document.getElementById("enter").value.trim();
var age = document.getElementById("age").value.trim();

if (enter == "" || age == "")
{
console.log("Check Function load");
alert('Please fill the Field');
return false;
}
else
{
// If there is no need to call the display function elsewhere,
// you can just paste its code directly here to prevent
// unnecessary function call
return display(enter, age);
}
}


function display(enter, age)
{
data.push(enter, age);
document.getElementById("data_table").innerHTML += "<tr><td>" + enter + "</td><td>" + age + "</td> <td></td></tr>";

}

关于javascript - 我必须将数组数据打印到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54665488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com