2017-11-13 4 views
0

Ich versuche, jeder Zeile, die dynamisch eine eindeutige ID hinzugefügt wird. Grundsätzlich, indem jedes Mal, wenn der Benutzer auf die Schaltfläche Hinzufügen klickt, die Zahl zu der Zahl hinzugefügt wird. Es fügt eine ID hinzu, aber nicht korrekt, es wird in den Entwicklungstools als "undefiniert" angezeigt.JavaScript setAttribute ID zu Zeile ist nicht definiert

var counter = 0; 
 
function appendRow(id, style) { 
 
    var table = document.getElementById(id); // table reference 
 
    length = table.length, 
 
    row = table.insertRow(table.rows.length, 'id');  // append table row 
 
    row.setAttribute('id', style); 
 
    row.setAttribute('idName', style); 
 
    var i; 
 
    // insert table cells to the new row 
 
    for (i = 0; i < table.rows[0].cells.length; i++) { 
 
     createCell(row.insertCell(i), i, 'cust' + counter); 
 
     counter++ 
 
    } 
 
} 
 

 
function createCell(cell, text, style) { 
 
    var div = document.createElement('div'), // create DIV element 
 
     txt = document.createTextNode('_'); // create text node 
 
    div.appendChild(txt);    // append text node to the DIV 
 
    div.setAttribute('id', style);  // set DIV class attribute 
 
    div.setAttribute('idName', style); // set DIV class attribute for IE (?!) 
 
    cell.appendChild(div);     // append DIV to the table cell 
 
}
table { 
 
    text-align: center; 
 
} 
 
td { 
 
    width: 100px; 
 
} 
 

 

 
tr:nth-child(even) { 
 
    background-color: #fff; 
 
} 
 
tr:nth-child(odd) { 
 
    background-color: #eee; 
 
}
<button id="addCust" class="addSort" onclick="appendRow('custList')">add customer</button> 
 
<div class="custScroll"> 
 
     <table id="custListTop" contenteditable="false"> 
 
      <tr> 
 
      <td style="border-top-left-radius: 5px;">Customers</td> 
 
      <td style="border-top-right-radius: 5px;">Main Location</td> 
 
      </tr> 
 
     </table> 
 
     <table id="custList" contenteditable="true"> 
 
      <tr> 
 
      <td>Someone</td> 
 
      <td>Somewhere</td> 
 
      </tr> 
 
     </table> 
 
     </div>

+2

Verwenden Sie '.id' anstelle von' .setAttribute ('id', ...) '. Und was ist "Stil"? CSS ??? –

+1

Sie scheinen nur ein Argument an 'appendRow()' zu übergeben, obwohl es zwei dauert. Dies führt dazu, dass "Stil" undefiniert ist. – abagshaw

+0

@ibrahimmahrir Es gibt kein Problem mit 'setAttibute' mit' id', wenn Sie möchten. –

Antwort

0

Der Grund, warum die neuen Elemente als „undefiniert“ zeigen sich ist, weil der Stil Argument von appendRow nicht zur Verfügung gestellt worden.

Um die Funktionalität zu erhalten, müssen Sie den Stil aus den appendRow-Argumenten entfernen und die Referenzen auf Stil in appendRow durch 'cust' + counter ersetzen.

0

ist Ihr Stil Wert hier null bitte Stil Wert überprüfen habe ich auch Geige hinzugefügt diesen Code

Bitte überprüfen Sie, wenn Benutzer auf die Schaltfläche ist klickt der Stil Wert undefiniert.

<button id="addCust" class="addSort" ***onclick="appendRow('custList')"***>add customer</button> 

appendRow Funktion erfordert zwei Parameter und Sie nur auf eine.

var counter = 0; 
 
$('#addCust').click(function() { 
 
    var table = document.getElementById('custListTop'); // table reference 
 
    length = table.length, 
 
    row = table.insertRow(table.rows.length, 'id'); // append table row 
 
    row.setAttribute('id', counter); 
 
    row.setAttribute('idName', counter); 
 
    var i; 
 
    // insert table cells to the new row 
 
    for (i = 0; i < table.rows[0].cells.length; i++) { 
 
    createCell(row.insertCell(i), i, 'cust' + counter); 
 
    counter++ 
 
    } 
 
}); 
 

 

 
function createCell(cell, text, style) { 
 
    var div = document.createElement('div'), // create DIV element 
 
    txt = document.createTextNode('_'); // create text node 
 
    div.appendChild(txt); // append text node to the DIV 
 
    div.setAttribute('id', style); // set DIV class attribute 
 
    div.setAttribute('idName', style); // set DIV class attribute for IE (?!) 
 
    cell.appendChild(div); // append DIV to the table cell 
 
}
table { 
 
    text-align: center; 
 
} 
 

 
td { 
 
    width: 100px; 
 
} 
 

 
tr:nth-child(even) { 
 
    background-color: #fff; 
 
} 
 

 
tr:nth-child(odd) { 
 
    background-color: #eee; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id="addCust" class="addSort">add customer</button> 
 
<div class="custScroll"> 
 
    <table id="custListTop" contenteditable="false"> 
 
    <tr> 
 
     <td style="border-top-left-radius: 5px;">Customers</td> 
 
     <td style="border-top-right-radius: 5px;">Main Location</td> 
 
    </tr> 
 
    </table> 
 
    <table id="custList" contenteditable="true"> 
 
    <tr> 
 
     <td>Someone</td> 
 
     <td>Somewhere</td> 
 
    </tr> 
 
    </table> 
 
</div>

+0

Bitte geben Sie keinen Code für Websites von Drittanbietern ein, da diese Links im Laufe der Zeit abstürzen können. Fügen Sie einfach ein Code-Snippet hier in Ihre Antwort ein. –

Verwandte Themen