adding new rows to table on clicking button in jquery
I am pretty new to jquery..i have the following code. here i wants to get new rows in the table on clicking add button.. but i cant get it.,
can someone tell me what mistake i ve done here?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title> Adding Next Rows </title>
<link rel="stylesheet" href="resources/css/jquery.ui.all.css">
<script src="resources/scripts/jquery.ui.effect.js"></script>
<script src="resources/scripts/jquery.ui.effect-explode.js"></script>
<style>
h3
{
color:#0000FF;
display:inline-block;
}
.pdzn_tbl1 th
{
background:#DFDFDF;
border:#D6D6D6 1px solid;
}
.pdzn_tbl1 td
{
border-right:#729111 1px solid;
border-bottom:#729111 1px solid;
padding-top: 5px;
}
</style>
</head>
<body>
<h3> Adding Next Rows </h3>
<script type="text/javascript">
function ajax()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
return;
}
}
var $ = jQuery.noConflict();
$("#addrows").click(function () {
if(document.getElementById("hiddenprice").value == "") {
imagecounter = 4;
} else {
imagecounter = parseFloat(document.getElementById("hiddenprice").value) + 1;
}
//imagecounter=4;
var newImageDiv = $(document.createElement('div'))
.attr("id", 'add_div' + imagecounter);
/*onchange="return fn_productname(this.value,'prdname'+imagecounter+'')"*/
newImageDiv.after().html('<table width="100%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="0">'+
'<tr>'+
'<td style="padding:5px;" >'+'<input type="text" name="rollno<? $i ?>" />' + '</td>' + '<td style="padding:5px;">'+ '<input type="text" name="firstname<? $i ?>" />'+'</td>'+'<td style="padding:5px;">'+'<input type="text" name="lastname<? $i ?>" />'+'</td>'+'</tr>'+'</table>');
newImageDiv.appendTo("#addgroup");
$("tr:last").after(newImageDiv);
document.getElementById("hiddenprice").value = imagecounter;
imagecounter++;
});
</script>
<div class="common" style="width:1040px; -overflow-x:scroll; padding: 5px 5px 0 5px;">
<table id="maintable" width="50%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="#729111 1px solid" >
<tr> <th align="center"> Roll No </th>
<th align="center"> First Name </th>
<th align="center"> Last Name </th>
</tr>
<?php
$t_row=3;
for($i=1;$i<=$t_row;$i++)
{
?>
<tr id="rows">
<div style="padding-left: 5px">
<td style="padding:5px;" > <input type="text" name="rollno<? $i ?>" /> </td>
<td style="padding:5px;"> <input type="text" name="firstname<? $i ?>" /> </td>
<td style="padding:5px;"> <input type="text" name="lastname<? $i ?>" /> </td>
</tr>
</div>
<? } ?>
<div id="addgroup">
<div id="add_div1"> </div>
</div> <table>
<br />
<input type="button" name="add" value="+Add" id="addrows" style="color:#3300FF; font-size:16px; " />
<input type="hidden" id="hiddenprice" name="hiddenprice" value="3"/> </table>
</div>
</body>
</html>
thanks in advance..!!!!
Sample DEMO for Adding new row
$("#addrows").click(function () {
$("#mytable").each(function () {
var tds = '<tr>';
jQuery.each($('tr:last td', this), function () {
tds += '<td>' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
Updated: Here div close at wrong place, it should end before tr close, may be thats the error
<tr id="rows">
<div style="padding-left: 5px">
<td style="padding:5px;" > <input type="text" name="rollno<? $i ?>" /> </td>
<td style="padding:5px;"> <input type="text" name="firstname<? $i ?>" /> </td>
<td style="padding:5px;"> <input type="text" name="lastname<? $i ?>" /> </td>
</div> // right
</tr>
</div> // wrong
UPDATED DEMO 2
Have a look at Add table row in jQuery
which gives the solution
$('#maintable tr:last').after('<tr><td>...</td><td>...</td><td>...</td><td>...</td></tr>');
As explained here a solution with after
is to be preferred over append
.
Notes
jquery
with the approach with getElementById
. Demo code
http://jsfiddle.net/A5dT6/1/
尝试像这样,FIDDLE
$(function () {
$("#addRows").click(function () {
$("#maintable").append("<tr> <td> New Row</td> </tr>")
});
})
链接地址: http://www.djcxy.com/p/22986.html
上一篇: 表与twitter引导和jQuery
下一篇: 在jquery中单击按钮的表上添加新行