質問編集履歴

1

コード記載

2018/02/19 05:12

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -16,9 +16,273 @@
16
16
 
17
17
 
18
18
 
19
- コードはjsFiddleに記載しましたので、ご確認ください。
20
-
21
-
19
+ ###コード
20
+
21
+ ```html
22
+
23
+ <div style="padding-top:40px;">
24
+
25
+ <table style="width: 90%;" class="invoice-table">
26
+
27
+ <thead>
28
+
29
+ <tr class="billing-title">
30
+
31
+ <th style="border:none;background-color: #ffffff;"></th>
32
+
33
+ <th>品番・品名</th>
34
+
35
+ <th>数量、単位</th>
36
+
37
+ <th>単価</th>
38
+
39
+ <th>金額</th>
40
+
41
+ </tr>
42
+
43
+ </thead>
44
+
45
+ <tbody>
46
+
47
+ <tr class="table-solid table-input tbl-cnt">
48
+
49
+ <td class="hide-td">
50
+
51
+ <div class="hide-btn delete-btn"><span href="#" name="delete" class="delete btn btn-success"><i class="fa fa-times fa-fw"></i></span></div>
52
+
53
+ </td>
54
+
55
+ <td><input type="text" name="item" class="item" style="margin:10px;width: 450px;"></td>
56
+
57
+ <td><input type="text" name="num" class="num" style="margin:10px;"></td>
58
+
59
+ <td><input type="text" name="unit-price" class="unit-price" style="margin:10px;"></td>
60
+
61
+ <td><input type="text" name="price" class="price" style="margin:10px;"></td>
62
+
63
+ </tr>
64
+
65
+ <tr class="table-solid table-input tbl-cnt">
66
+
67
+ <td class="hide-td">
68
+
69
+ <div class="hide-btn delete-btn"><span href="#" name="delete" class="delete btn btn-success"><i class="fa fa-times fa-fw"></i></span></div>
70
+
71
+ </td>
72
+
73
+ <td><input type="text" name="item" class="item" style="margin:10px;width: 450px;"></td>
74
+
75
+ <td><input type="text" name="num" class="num" style="margin:10px;"></td>
76
+
77
+ <td><input type="text" name="unit-price" class="unit-price" style="margin:10px;"></td>
78
+
79
+ <td><input type="text" name="price" class="price" style="margin:10px;"></td>
80
+
81
+ </tr>
82
+
83
+ <tr class="table-solid table-input tbl-cnt">
84
+
85
+ <td class="hide-td">
86
+
87
+ <div class="hide-btn delete-btn"><span href="#" name="delete" class="delete btn btn-success"><i class="fa fa-times fa-fw"></i></span></div>
88
+
89
+ </td>
90
+
91
+ <td><input type="text" name="item" class="item" style="margin:10px;width: 450px;"></td>
92
+
93
+ <td><input type="text" name="num" class="num" style="margin:10px;"></td>
94
+
95
+ <td><input type="text" name="unit-price" class="unit-price" style="margin:10px;"></td>
96
+
97
+ <td><input type="text" name="price" class="price" style="margin:10px;"></td>
98
+
99
+ </tr>
100
+
101
+ <tr class="table-solid table-input tbl-cnt">
102
+
103
+ <td class="hide-td">
104
+
105
+ <div class="hide-btn delete-btn"><span href="#" name="delete" class="delete btn btn-success"><i class="fa fa-times fa-fw"></i></span></div>
106
+
107
+ </td>
108
+
109
+ <td><input type="text" name="item" class="item" style="margin:10px;width: 450px;"></td>
110
+
111
+ <td><input type="text" name="num" class="num" style="margin:10px;"></td>
112
+
113
+ <td><input type="text" name="unit-price" class="unit-price" style="margin:10px;"></td>
114
+
115
+ <td><input type="text" name="price" class="price" style="margin:10px;"></td>
116
+
117
+ </tr>
118
+
119
+ <tr class="table-solid table-input">
120
+
121
+ <td style="border:none;"></td>
122
+
123
+ <td style="border:none;text-align: center;">
124
+
125
+ <span href="#" id="add-row" class="btn btn-success" style="margin-top: 5px;padding: 3px 3px; font-size: 11px;">
126
+
127
+ <i class="fa fa-plus fa-fw"></i>行の追加<span style="font-size:10px;">※最大で20行まで増やせます。</span>
128
+
129
+ </span>
130
+
131
+ </td>
132
+
133
+ <td style="border: 1px solid #bab9b9;text-align:center;height: 40px;" colspan="2">小計</td>
134
+
135
+ <td><input type="text" name="tiny-sum" class="tiny-sum" style="margin:10px;" readonly="readonly"></td>
136
+
137
+ </tr>
138
+
139
+ <tr class="table-solid table-input">
140
+
141
+ <td style="border:none;"></td>
142
+
143
+ <td style="border:none;"></td>
144
+
145
+ <td style="border: 1px solid #bab9b9;text-align:center;height: 40px;" colspan="2">消費税(8%)</td>
146
+
147
+ <td><input type="text" name="tax" class="tax" style="margin:10px;" readonly="readonly"></td>
148
+
149
+ </tr>
150
+
151
+ <tr class="table-solid table-input">
152
+
153
+ <td style="border:none;"></td>
154
+
155
+ <td style="border:none;"></td>
156
+
157
+ <td style="border: 1px solid #bab9b9;text-align:center;height: 40px;" colspan="2">合計</td>
158
+
159
+ <td><input type="text" name="all-sum" class="all-sum" style="margin:10px;" readonly="readonly"></td>
160
+
161
+ </tr>
162
+
163
+ </tbody>
164
+
165
+ </table>
166
+
167
+ </div>
168
+
169
+ ```
170
+
171
+ ```
172
+
173
+ $(function(){
174
+
175
+ $('table tbody tr.tbl-cnt').each(function(i){
176
+
177
+ $(this).find('.delete').attr("name", 'delete' + (i+1));
178
+
179
+ $(this).find('.item').attr("name", 'item' + (i+1));
180
+
181
+ $(this).find('.num').attr("name", 'num' + (i+1));
182
+
183
+ $(this).find('.unit-price').attr("name", 'unit-price' + (i+1));
184
+
185
+ $(this).find('.price').attr("name", 'price' + (i+1));
186
+
187
+ // 計算処理 フォーカスが外れたとき発生
188
+
189
+ var all;
190
+
191
+ $(this).keyup(function() {
192
+
193
+ all = 0;
194
+
195
+ var num =$(this).find('.num').val();
196
+
197
+ var unit_price = $(this).find('.unit-price').val();
198
+
199
+ var price = $(this).find('.price');
200
+
201
+ if(num !== "" && unit_price !== "") {
202
+
203
+ sum = num*unit_price;
204
+
205
+ price.val(sum);
206
+
207
+ $('.price').each(function() {
208
+
209
+ if($(this).val() !== "") {
210
+
211
+ all += sum + all;
212
+
213
+ }
214
+
215
+ });
216
+
217
+ $('input[name=tiny-sum]').val(all);console.log(all);
218
+
219
+ }
220
+
221
+ });
222
+
223
+ });
224
+
225
+ $('table').on('click', '#add-row', function() {
226
+
227
+ var lenNum = $("table tbody").children().length;
228
+
229
+ var namae = $("table tbody tr.tbl-cnt:last input[type=text]").attr('name');
230
+
231
+ var namaeNum = namae.replace(/[^0-9^.]/g,"");
232
+
233
+ // 20個以上に行を増やすことはできない
234
+
235
+ if(lenNum - 2 < 21) {
236
+
237
+ var deleteName = 'delete' + (parseInt(namaeNum) + 1);
238
+
239
+ var itemName = 'item' + (parseInt(namaeNum) + 1);
240
+
241
+ var numName = 'num' + (parseInt(namaeNum) + 1);
242
+
243
+ var unit_priceName = 'unit-price' + (parseInt(namaeNum) +1);
244
+
245
+ var priceName = 'price' + (parseInt(namaeNum) +1);
246
+
247
+ $('table tbody tr.tbl-cnt:last')
248
+
249
+ .after('<tr class="table-solid table-input tbl-cnt"><td class="hide-td"><div class="hide-btn delete-btn"><span href="#" name="'+ deleteName +'" class="delete btn btn-success"><i class="fa fa-times fa-fw"></i></span></div></td><td><input type="text" name="'+ itemName +'" class="item" style="margin:10px;width: 450px;"></td><td><input type="text" name="'+ numName +'" class="num" style="margin:10px;"></td><td><input type="text" name="'+ unit_priceName +'" class="unit-price" style="margin:10px;"></td><td><input type="text" name="'+ priceName +'" class="price" style="margin:10px;"></td></tr>');
250
+
251
+ } else {
252
+
253
+ alert('最大追加可能行数の20に達しました。\nこれ以上追加することはできません。');
254
+
255
+ return false;
256
+
257
+ }
258
+
259
+ });
260
+
261
+ $('table').on('click', '.delete', function() {
262
+
263
+ var len = $("table tbody").children().length;
264
+
265
+ if(len - 2 > 2) {
266
+
267
+ var row = $(this).closest("tr").remove();
268
+
269
+ $(row).remove();
270
+
271
+ } else {
272
+
273
+ alert('これ以上行を削除することはできません。');
274
+
275
+ return false;
276
+
277
+ }
278
+
279
+ });
280
+
281
+ });
282
+
283
+
284
+
285
+ ```
22
286
 
23
287
  ###jsFiddle
24
288