jquery tr 동적생성(clone)

2015. 3. 24. 13:03IT/Jquery Plugin/Tip

<?
include_once("./_common.php");
include_once("./head.sub.php");
?>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
    <td height="33" bgcolor="#ffffff">학생정보 입력</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
    <td bgcolor="#CCCCCC">
        <TABLE cellSpacing=1 cellPadding=0 width=100% id="example">
<TR height="20" align="center">
<TD bgcolor="#f7f7f7">성명</TD>
<TD bgcolor="#f7f7f7">핸드폰번호</TD>
<TD bgcolor="#f7f7f7">주민등록번호</TD>
<TD bgcolor="#f7f7f7">보호자와의 관계</TD>
<TD bgcolor="#f7f7f7"><button id="addItemBtn">옵션추가</button></TD>
</TR>
<? if(strlen($member[ch_no]) > 0) {
for($j = 0; $j < count($member[ch]);$j++){
?>
<TR height="20" align="center">
<TD bgcolor="#ffffff"><input type="text" name="ch_no[]" value="<?=$member[ch][$j][ch_no]?>"><input type="text" name="ch_nm[]" value="<?=$member[ch][$j][ch_nm]?>" style='width:60px;'></TD>
<TD bgcolor="#ffffff"><input type="text" name="ch_hp1[]" value="<?=$member[ch][$j][ch_hp1]?>" maxlength="6" style='width:40px;'>-<input type="text" name="ch_hp2[]" value="<?=$member[ch][$j][ch_hp2]?>" maxlength="4" style='width:50px;'>-<input type="text" name="ch_hp3[]" value="<?=$member[ch][$j][ch_hp3]?>" maxlength="4" style='width:50px;'></TD>
<TD bgcolor="#ffffff"><input type="text" name="ch_jumin1[]" value="<?=$member[ch][$j][ch_jumin1]?>" maxlength="6" style='width:60px;'>-<input type="text" name="ch_jumin2[]" value="<?=$member[ch][$j][ch_jumin2]?>" maxlength="7" style='width:60px;'></TD>
<TD bgcolor="#ffffff"><input type="text" name="ch_rel[]" value="<?=$member[ch][$j][ch_rel]?>" style='width:80px;'></TD>
<TD bgcolor="#ffffff">&nbsp;</TD>
</TR>
<?  }  ?>
<? } ?>
<TR height="20" align="center">
<TD bgcolor="#ffffff"><input type="text" name="ch_no[]" value=""><input type="text" name="ch_nm[]" value="" style='width:60px;'></TD>
<TD bgcolor="#ffffff"><input type="text" name="ch_hp1[]" value="" maxlength="6" style='width:40px;'>-<input type="text" name="ch_hp2[]" value="" maxlength="4" style='width:50px;'>-<input type="text" name="ch_hp3[]" value="" maxlength="4" style='width:50px;'></TD>
<TD bgcolor="#ffffff"><input type="text" name="ch_jumin1[]" value="" maxlength="6" style='width:60px;'>-<input type="text" name="ch_jumin2[]" value="" maxlength="7" style='width:60px;'></TD>
<TD bgcolor="#ffffff"><input type="text" name="ch_rel[]" value="" style='width:80px;'></TD>
<TD bgcolor="#ffffff">&nbsp;</TD>
</TR>
</table>
</td>
</tr>
</table>


<script language="JavaScript">
$("#addItemBtn").click(function(){
// item 의 최대번호 구하기
var lastItemNo = $("#example tr:last").attr("class").replace("item", "");
var newitem = $("#example tr:eq(1) ").clone();
newitem.removeClass();
newitem.find("td:eq(0)").attr("rowspan", "1");

newitem.addClass("item"+(parseInt(lastItemNo)+1));
 
                $("#example").append(newitem);

$("#example tr:last td input[name='ch_no[]']").val('');
$("#example tr:last td input[name='ch_nm[]']").val('');
$("#example tr:last td input[name='ch_hp1[]']").val('');
$("#example tr:last td input[name='ch_hp2[]']").val('');
$("#example tr:last td input[name='ch_hp3[]']").val('');
$("#example tr:last td input[name='ch_jumin1[]']").val('');
$("#example tr:last td input[name='ch_jumin2[]']").val('');
$("#example tr:last td input[name='ch_rel[]']").val('');

            });
</script>


<?include_once("./tail.sub.php");?>