Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Javascript add row

  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2011
    Messaggi
    2

    Javascript add row

    Ciao a tutti sono nuovo e soprattutto sono un completo neofita per quanto riguarda il javascript.
    Arrivo al dunque.

    Ho questo script lato js

    codice:
        function addRow(tableID) {
    
            var table = document.getElementById(tableID);
    
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
    
            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            cell1.appendChild(element1);
    
            var cell2 = row.insertCell(1);
            cell2.innerHTML = rowCount + 0;
    
            var cell3 = row.insertCell(2);
            var element2 = document.createElement("input");
            element2.type = "text";
            cell3.appendChild(element2);
    
            var cell4 = row.insertCell(3);
            var element3 = document.createElement("input");
            element3.type = "text";
            cell4.appendChild(element3);
    
        }
    
        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
    
            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }
    
            }
            }catch(e) {
                alert(e);
            }
        }
    Lato html invece questo

    codice:
            <input type="button" value="Aggiungi riga" onclick="addRow('scadenzeTable')" /> 
            <input type="button" value="Elimina riga" onclick="deleteRow('scadenzeTable')" /> 
    
            <table id="scadenzeTable" width="350px" border="1"> 
                <tr> 
                    <th>Seleziona</th> 
                    <th>Riga numero</th> 
                    <th>Data</th> 
                    <th>Descrizione</th> 
                </tr> 
                    <tr> 
                        <td><input type="checkbox" name="chk"/></td> 
                        <td> 1 </td> 
                        <td><input type="text" /></td> 
                        <td><input type="text" /></td> 
                    </tr> 
            </table>
    Come codice mi va bene, ma fa troppo per quel che mi serve.
    Vorrei togliere la funzione per eliminare la riga e le prime due colonne della tabella in modo che rimanga solo la colonna "Data" e la colonna "Descrizione" e aggiungere quindi i soli due campi di input quando premo il pulsante "Aggiungi riga".
    Il problema è che non ci riesco non capisco cosa devo togliere o come devo adattare .
    Grazie
    Juns

  2. #2

    Re: Javascript add row

    codice:
        function addRow(tableID) {
    
            var table = document.getElementById(tableID);
    
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
    
            var cell3 = row.insertCell(0);
            var element2 = document.createElement("input");
            element2.type = "text";
            cell3.appendChild(element2);
    
            var cell4 = row.insertCell(1);
            var element3 = document.createElement("input");
            element3.type = "text";
            cell4.appendChild(element3);
    
        }
    codice:
            <input type="button" value="Aggiungi riga" onclick="addRow('scadenzeTable')" /> 
            <table id="scadenzeTable" width="350px" border="1"> 
                <tr> 
                    <th>Data</th> 
                    <th>Descrizione</th> 
                </tr> 
                    <tr> 
                        <td><input type="text" /></td> 
                        <td><input type="text" /></td> 
                    </tr> 
            </table>

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2011
    Messaggi
    2
    grazie infinite
    Juns

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.