Javascript 在添加新内容之前检查表内容

Javascript 在添加新内容之前检查表内容,javascript,html-table,Javascript,Html Table,我正在尝试创建一个存储用户的表。 我能够构建的是一个表,您可以在其中添加和删除一些用户。 现在,我尝试在添加行之前检查内容。 如果我执行该函数,我的表将以指数形式增长,而不是在添加新用户之前检查内容 // push on the button excecutes addRow function addRow(){ var firstName=document.getElementById("firstName").value; var surName=document

我正在尝试创建一个存储用户的表。 我能够构建的是一个表,您可以在其中添加和删除一些用户。 现在,我尝试在添加行之前检查内容。 如果我执行该函数,我的表将以指数形式增长,而不是在添加新用户之前检查内容

  //    push on the button excecutes addRow
function addRow(){

    var firstName=document.getElementById("firstName").value;
    var surName=document.getElementById("surName").value;
    var email=document.getElementById("email").value;
    compareRows(document.getElementById('Gebruikers'));

}

//first i want to compare content of rows vs the inputfields

function compareRows(){
    var table=document.getElementById("Gebruikers");
    var rowCount=table.rows.length;

//check first cell of the row. when it is the same as firstName check surName
        for(var i=0;i<rowCount;i++) {
            var row=table.rows[i];
            var vCheck=row.cells[0];
            if(vCheck===firstName) {

//check second cell of the row. when it is the same as surName check email              
                for(var i=0;i<rowCount;i++) {
                    var row=table.rows[i];
                    var vCheck=row.cells[1];
                    if (aCheck===surName) {

//check third cell of the row. when it is the same as email show prompt that use allready exists.
//if email doesnt exist check next row  
                        for(var i=0;i<rowCount;i++) {
                            var row=table.rows[i];
                            var eCheck=row.cells[2];
                            if (eCheck===email) {
                                prompt ("Deze gebruiker bestaat al!")
                            };
                        };
                    };
                };

//when all rows have been checked for input execute insertRow function
            } else {
            insertRow(firstName, surName, email);
            };
        } 
};


function insertRow(firstName, surName, email)
{
    var row = document.createElement("tr");
    var table = document.getElementById("Gebruikers");

    table.appendChild(row);

    row.appendChild(createTd(firstName));
    row.appendChild(createTd(surName));
    row.appendChild(createTd(email));

    var cell4=row.insertCell(3);
    var remove=document.createElement("input");
    remove.type="checkbox";
    remove.name="chkbox[]";
    cell4.appendChild(remove);
}

function createTd(text) {
    var td = document.createElement("td");
    td.innerText = text;
    return td;
}


function deleteRow(){
    var table=document.getElementById("Gebruikers");
    var rowCount=table.rows.length;
        for(var i=0;i<rowCount;i++) {
            var row=table.rows[i];
            var chkbox=row.cells[3].childNodes[0];
                if(null!=chkbox&&true==chkbox.checked) {
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }
            }
        }
//按此按钮可删除addRow
函数addRow(){
var firstName=document.getElementById(“firstName”).value;
var姓氏=document.getElementById(“姓氏”).value;
var email=document.getElementById(“email”).value;
compareRows(document.getElementById('Gebruikers');
}
//首先,我想比较行的内容和输入字段
函数compareRows(){
var table=document.getElementById(“Gebruikers”);
var rowCount=table.rows.length;
//检查行的第一个单元格。当与firstName相同时,检查姓氏

对于(var i=0;i,从我所看到的,在循环体中仍然有对insertRow函数的调用,这意味着如果它们不匹配,它将添加表中已有的行数。我要做的是添加某种变量,如果比较行将返回true,则会更改该变量

var temp = 0; if(all_rows=true){temp++;}
在循环之后,我要添加:

if(temp > 0) {insertRow();}
所以应该是这样的:

function compareRows(){
    var table=document.getElementById("Gebruikers");
    var rowCount=table.rows.length;
var temp = 0;
//check first cell of the row. when it is the same as firstName check surName
        for(var i=0;i<rowCount;i++) {
            var row=table.rows[i];
            var vCheck=row.cells[0];
            if(vCheck===firstName) {

//check second cell of the row. when it is the same as surName check email              
                for(var i=0;i<rowCount;i++) {
                    var row=table.rows[i];
                    var vCheck=row.cells[1];
                    if (aCheck===surName) {

//check third cell of the row. when it is the same as email show prompt that use allready exists.
//if email doesnt exist check next row  
                        for(var i=0;i<rowCount;i++) {
                            var row=table.rows[i];
                            var eCheck=row.cells[2];
                            if (eCheck===email) {
                                prompt ("Deze gebruiker bestaat al!")
                                temp++;
                            };
                        };
                    };
                };

//when all rows have been checked for input execute insertRow function
            } else {

            };

        } 
        if(temp > 0)
        {
         insertRow(firstName, surName, email);
         }
};
函数比较器ows(){
var table=document.getElementById(“Gebruikers”);
var rowCount=table.rows.length;
var-temp=0;
//检查行的第一个单元格。当与firstName相同时,检查姓氏

对于(var i=0;i)当我将其从循环中取出时,它不会执行insertRow函数。当我将其保持在循环中时,它将以指数形式添加。单元格的内容始终为:“[object HTMLInputElement]”。实际上,您可以用一种更简单的方法来完成所有这些。基本上应该有两个循环。首先,您在所有行上执行一个循环,在这个循环中,如果第一个选中元素的值被复制,那么您将循环处理其余行元素,以确保行中的所有元素都被复制。如果是这样,您将设置一个标志(变量)对于1,它不会添加一行,否则它会添加一行。您能告诉我们您使用的html代码,您使用的是什么类型的元素吗?至于单元格值,因为您在单元格中的值是输入的,所以您应该调用cell.val()或某物。var vCheck=row.cells[0].value;不会改变它。你能告诉我你到底使用什么js框架吗?
function compareRows(){
    var table=document.getElementById("Gebruikers");
    var rowCount=table.rows.length;
var temp = 0;
//check first cell of the row. when it is the same as firstName check surName
        for(var i=0;i<rowCount;i++) {
            var row=table.rows[i];
            var vCheck=row.cells[0];
            if(vCheck===firstName) {

//check second cell of the row. when it is the same as surName check email              
                for(var i=0;i<rowCount;i++) {
                    var row=table.rows[i];
                    var vCheck=row.cells[1];
                    if (aCheck===surName) {

//check third cell of the row. when it is the same as email show prompt that use allready exists.
//if email doesnt exist check next row  
                        for(var i=0;i<rowCount;i++) {
                            var row=table.rows[i];
                            var eCheck=row.cells[2];
                            if (eCheck===email) {
                                prompt ("Deze gebruiker bestaat al!")
                                temp++;
                            };
                        };
                    };
                };

//when all rows have been checked for input execute insertRow function
            } else {

            };

        } 
        if(temp > 0)
        {
         insertRow(firstName, surName, email);
         }
};