我有一个html表单,点击按钮时,需要启动javascript函数,并且需要插入表中的新行。 任何人都可以帮助我,我可以如何正确组装javascript代码请。我有代码,但无法正确组装。从html按钮启动javascript功能
下面是代码:
<SCRIPT language="javascript">
function addRow()
{
var table = document.getElementById('table');
var button = document.getElementsByTagName('input')[0];
button.onclick = function() {
var clone = table.rows[table.rows.length - 2].cloneNode(true);
clone.cells[0].firstChild.data =
clone.cells[0].firstChild.data.replace(/(\d+):/,function(str,g1) {
return (+g1 + 1) + ':';
});
table.tBodies[0].appendChild(clone);
};
}
</SCRIPT>
和HTML的一部分:
<input type=button value='Change' onclick="addRow" />
<br /><br />
<table cellspacing=0 cellpadding=4 id="table">
<tbody>
<tr><td>Number 1:</td><td><input value=20 style="width:30px" type=text maxlength=2/></td></tr>
<tr><td>Number 2:</td><td><input value=25 style="width:30px" type=text maxlength=2/></td></tr>
<tr><td style="border-top:solid 1px black;border-bottom:solid 1px black;">Sum:</td><td style="border-top:solid 1px black;border-bottom:solid 1px black;"><div>45</div></td></tr>
</tbody>
<tfoot>
<tr><td style="border-top:solid 1px black;border-bottom:solid 1px black;">Sum:</td><td style="border-top:solid 1px black;border-bottom:solid 1px black;"><div>45</div></td></tr>
</tfoot>
</table>
由于提前,Laziale
“按钮”是JavaScript中的保留字。将它重命名为另一个变量。请参阅:http://www.javascripter.net/faq/reserved.htm – 2012-01-13 16:32:50