행이나 열을 동적으로 추가하고 싶다면 다음 소스를 이용하자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 자바스크립트</TITLE>
</HEAD>
<BODY>
<input type=button value="행추가" onclick="add_row();">
<input type=button value="열추가" onclick="add_col();">
<input type=button value="행삭제" onclick="del_row();">
<input type=button value="열삭제" onclick="del_col();">
<table id='stock_table' border=1>
<thead id='stock_thead'>
<tr>
<td>
<input type=text name=opty size=4>
<input type=text name=optx size=4>
</td>
<td>
<input type=text name='optx_subj[]' size=10>
</td>
</tr>
</thead>
<tbody id='stock_tbody'>
<tr>
<td><input type=text name='opty_subj[]' size=10></td>
<td><input type=text name='opt[0][]' size=10></td>
</tr>
</tbody>
</table>
<input type=submit>
</form>
<span id=msg></span>
<script>
var stock_table = document.getElementById('stock_table');
var stock_thead = document.getElementById('stock_thead');
var stock_tbody = document.getElementById('stock_tbody');
var stock_tr = null;
var stock_td = null;
var stock_x = 0;
var stock_y = 0;
var msg = document.getElementById('msg');
function add_row()
{
// 행 제목 추가
stock_tr = document.createElement('tr');
stock_tbody.appendChild(stock_tr);
stock_td = document.createElement('td');
stock_tr.appendChild(stock_td);
add_input('opty_subj[]');
stock_y++;
// 열의 갯수에 따라 추가된 행의 열 추가
var stock_thead_td = stock_thead.getElementsByTagName('td');
for (i=0; i<stock_thead_td.length-1; i++)
{
stock_td = document.createElement('td');
stock_tr.appendChild(stock_td);
add_input('opt['+stock_y+'][]');
}
}
function add_col()
{
// 열 제목 추가
var stock_thead_tr = stock_thead.getElementsByTagName('tr');
stock_td = document.createElement('td');
stock_thead_tr[0].appendChild(stock_td);
add_input('optx_subj[]');
var stock_tbody_tr = stock_tbody.getElementsByTagName('tr');
for (i=0; i<stock_tbody_tr.length; i++) {
stock_td = document.createElement('td');
stock_tbody_tr[i].appendChild(stock_td);
add_input('opt['+i+'][]');
}
}
function add_input(name)
{
var inp = document.createElement('input');
inp.setAttribute('type', 'text');
inp.setAttribute('size', '10');
inp.setAttribute('name', name);
stock_td.appendChild(inp);
}
function del_row()
{
var stock_tbody_tr = stock_tbody.getElementsByTagName('tr');
if (stock_tbody_tr.length > 1) {
stock_tbody.deleteRow(stock_tbody_tr.length-1);
stock_y--;
}
}
function del_col()
{
var stock_thead_tr = stock_thead.getElementsByTagName('tr');
var stock_thead_td = stock_thead.getElementsByTagName('td');
if (stock_thead_td.length > 2) {
stock_thead_tr[0].deleteCell(stock_thead_td.length-1);
var stock_tbody_tr = stock_tbody.getElementsByTagName('tr');
for (i=0; i<stock_tbody_tr.length; i++) {
var tr_td = stock_tbody_tr[i].getElementsByTagName('td');
stock_tbody_tr[i].deleteCell(tr_td.length-1);
}
}
}
</script>
</BODY>
</HTML>
|
'IT기술 > javascript' 카테고리의 다른 글
[jquery] 플로팅 배너 자동으로 따라다니는 jQuery 소스 (0) | 2018.10.06 |
---|---|
SELECT BOX 선택시 내용을 텍스트박스에 출력하기 (0) | 2018.10.06 |
Cannot read property 'getElementsByTagName' of null 오류 (0) | 2018.10.06 |
자바스크립트 이벤트 루프 강의 (0) | 2018.10.06 |
버튼 클릭하면 이미지 변경하기 (0) | 2018.10.06 |