更新:我似乎只需在主函数中添加排序多个阵列同时
document.getElementById('fullName').focus();
NameSalaries.sort();
}
有固定它。我无法相信它确实有效。但我认为这是因为我压缩了主阵列。感谢大家的帮助!
好吧,我正在尝试一种新方法。这样,员工每次都会动态添加新的表格单元格。然而,我遇到的问题依然存在。我需要结果自己排序,因为他们提交。名字和薪水。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Table Entry </title>
</head>
<body>
<input type="text" id="fullName" value="" placeholder="Firstname"> :Enter employee name <br>
<input type="text" id="salary" value="" placeholder="Salary"> :Enter employee salary <br>
<input type="button" value="click me add to array" onclick="addToArray()"> <p> </p>
<table border="1" id="source" cellspacing="3" cellpadding="3">
<thead>
<tr> <th>Employee Name</th> <th>Employee Salary</th> </tr>
</thead>
<tbody id="sourceBody">
<tr><td> </td><td> </td></tr>
</tbody>
</table>
<script type="text/javascript">
var NameSalaries= [];
function addToArray() {
var fn = document.getElementById('fullName').value;
var sly = document.getElementById('salary').value;
var both = [fn,sly];
NameSalaries.push(both);
createBody(NameSalaries);
document.getElementById('fullName').focus();
}
function createBody(NS) {
var tarr = [];
for (var i=0; i<NS.length; i++) {
tarr.push('<tr><td>'+NS[i][0]+'</td><td>'+NS[i][1]+'</td></tr>');
} document.getElementById('sourceBody').innerHTML = tarr.join('');
}
</script>
</body>
</html>
你需要** zip **然后排序。 – Bergi
@Bergi:我刚才说他应该结合数组项目,然后排序......我从来没有听说过你指的** zip **。我们在谈论同样的事情吗? –
这绝对是我想要做的。我尝试了concat,但它没有奏效。任何帮助?对不起,js –