Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Bom dia galera !
Estou com o seguinte problema:
Tenho um código ao qual quero deletar uma linha qualquer da minha tabela.
O problema é que não estou conseguindo deletar a linha corrente, somente consigo deletar a última linha !
Já tentei passar o valor do botão por this, por perenteElement, parentNode e nada..
Alguém poderia me ajudar ?
Abaixo segue o código
Javascript
<script language="JAVASCRIPT">
function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
// right cell
var cellRight = row.insertCell(0);
var el1 = document.createElement('input');
el1.type = 'text';
el1.name = 'txtRow' + iteration;
el1.id = 'txtRow' + iteration;
el1.size = 10;
cellRight.appendChild(el1);
var cellRight = row.insertCell(1);
var el1 = document.createElement('input');
el1.type = 'text';
el1.name = 'txtRow' + iteration;
el1.id = 'txtRow' + iteration;
el1.size = 10;
cellRight.appendChild(el1);
var cellRight = row.insertCell(2);
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'txtRow' + iteration;
el2.id = 'txtRow' + iteration;
el2.size = 10;
cellRight.appendChild(el2);
var cellRight = row.insertCell(3);
var el3 = document.createElement('input');
el3.type = 'text';
el3.name = 'txtRow' + iteration;
el3.id = 'txtRow' + iteration;
el3.size = 10;
cellRight.appendChild(el3);
var cellRight = row.insertCell(4);
var el4 = document.createElement('input');
el4.type = 'button';
el4.name = 'btn' + iteration;
el4.id = 'btn' + iteration;
el4.value = 'Excluir';
el4.onclick = removeRowFromTable;
cellRight.appendChild(el4);
}
function removeRowFromTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
function openInNewWindow(frm)
{
// open a blank window
var aWindow = window.open('', 'TableAddRowNewWindow',
'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
// set the target to the blank window
frm.target = 'TableAddRowNewWindow';
// submit
frm.submit();
}
function validateRow(frm)
{
var chkb = document.getElementById('chkValidate');
if (chkb.checked) {
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length - 1;
var i;
for (i=1; i<=lastRow; i++) {
var aRow = document.getElementById('txtRow' + i);
if (aRow.value.length <= 0) {
alert('Row ' + i + ' is empty');
return;
}
}
}
openInNewWindow(frm);
}
</script>
HTML
<form action="tableaddrow_nw.html" method="get">
<span id="spanOutput" style="border: 1px solid #000; padding: 3px;"> </span>
</p>
<table border="1" id="tblSample">
<tr>
<th>NF</th>
<th>Peso</th>
<th>Valor Estimado</th>
<th>Volume</th>
</tr>
<tr>
<td><input type="text" name="txtRow1" id="txtRow1" size="10"/></td>
<td><input type="text" name="txtRow2" id="txtRow2" size="10"/></td>
<td><input type="text" name="txtRow3" id="txtRow3" size="10"/></td>
<td><input type="text" name="txtRow4" id="txtRow4" size="10"/></td>
<td><input type="button" value="Add" onclick="addRowToTable();" />
<input type="button" value="Remove" onclick="removeRowFromTable();" />
<input type="button" value="Submit" onclick="validateRow(this.form);" />
<input type="checkbox" id="chkValidate" /> Validate Submit</td>
</tr>
</table>
</form>Carregando comentários...