Как показано ниже:
Скопируйте код кода следующим образом:
//Динамическое добавление строк
функция addRow(){
var table = document.getElementById("tableID");
var newRow = table.insertRow(); //Создаем новую строку
var newCell1 = newRow.insertCell(); //Создаем новую ячейку
newCell.innerHTML = "" //Содержимое внутри ячейки
newCell.setAttribute("align","center"); //Установим положение
}
//Динамически удаляем строки
функция deleteRow(){
вар rowIndex = event.srcElement.parentElement.parentElement.rowIndex;
var Styles = document.getElementById("tableID");
styles.deleteRow(rowIndex);
}
<html>
<голова>
<title></title>
</голова>
<тело>
<table id="testTbl" border=1>
<тр>
<тд>
Название продукта
</td>
<тд>
Количество продукта
</td>
<тд>
Цена за единицу продукта
</td>
</tr>
<тр>
<тд>
<выбрать имя="а">
<option value="Электронный">Электронный</option>
<option value="Электроприборы">Электроприборы</option>
</select></td>
<тд>
<input type="text" name="b">
</td>
<тд>
<input type="text" name="c">
</td>
</td>
</таблица>
<input type="button" name="Submit2" value="Add" onclick="addRow()">
<скрипт>
функция addRow(){
//добавляем строку
вар newTr = testTbl.insertRow();
//добавляем столбец
вар newTd0 = newTr.insertCell();
вар newTd1 = newTr.insertCell();
вар newTd2 = newTr.insertCell();
вар newTd3 = newTr.insertCell();
//Устанавливаем содержимое и атрибуты столбца
newTd0.innerText = document.all("a").options[document.all("a").selectedIndex].text;
newTd1.innerText = document.all("b").value;
newTd2.innerText = document.all("c").value;
newTd3.innerHTML= '<input type="button" name="del" value="Delete" onclick="del(this)">';
}
функцияdel(о)
{
вар t=document.getElementById('testTbl');
t.deleteRow(o.parentNode.parentNode.rowIndex)
}
</скрипт>
</тело>
</html>