我有三个工作位的javascript,我正在尝试合并...失败。我试图这样做,当你点击一个按钮,它知道两个字段有相同的条目。如果没有,它将在我的HTML表格中创建另一行信息。
document.getElementById("goRow").addEventListener("click", check)
function check () {
if (document.getElementById("firstName").value == document.getElementById("lastName").value){
prompt('First name can not be last');
}
else {
function newRow() {
var table = document.getElementById("myTable");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = document.getElementById("firstName").value;
cell2.innerHTML = document.getElementById("lastName").value;
cell3.innerHTML = document.getElementById("email").value;
cell4.innerHTML = " ";
}
}
就像我说的那样,所有不同的部分都是自己工作的,但是我不能让它们全部相互配合。
答案 0 :(得分:5)
为什么要尝试将函数定义为ELSE条件的一部分?你应该自己拥有它,并从ELSE中调用它:
function check() {
if (document.getElementById("firstName").value == document.getElementById("lastName").value) {
prompt('First name can not be last');
} else {
newRow();
}
}
function newRow() {
var table = document.getElementById("myTable");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = document.getElementById("firstName").value;
cell2.innerHTML = document.getElementById("lastName").value;
cell3.innerHTML = document.getElementById("email").value;
cell4.innerHTML = " ";
}
答案 1 :(得分:0)
window.onload() {
document.getElementById("goRow").addEventListener("click", check)
}
function check () {
if (document.getElementById("firstName").value == document.getElementById("lastName").value){
prompt('First name can not be last');
} else {
newRow();
}
}
function newRow() {
var table = document.getElementById("myTable");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = document.getElementById("firstName").value;
cell2.innerHTML = document.getElementById("lastName").value;
cell3.innerHTML = document.getElementById("email").value;
cell4.innerHTML = " ";
}