回答編集履歴

2

追記

2019/07/31 00:46

投稿

yambejp
yambejp

スコア114845

test CHANGED
@@ -87,3 +87,91 @@
87
87
 
88
88
 
89
89
  ```
90
+
91
+ # 追記
92
+
93
+ 最大数=5とした場合
94
+
95
+ ```javascript
96
+
97
+ <script>
98
+
99
+ const max=5;
100
+
101
+ $(function() {
102
+
103
+ setButton();
104
+
105
+ $('.addformbox').on('click',function() {
106
+
107
+ $('.box:last').after($('.box:last').clone(true).find('.toiawase').val("").end());
108
+
109
+ renumber();
110
+
111
+ setButton();
112
+
113
+ });
114
+
115
+ $('.deletformbox').on('click',function() {
116
+
117
+ $(this).parents(".box").remove();
118
+
119
+ renumber();
120
+
121
+ setButton();
122
+
123
+ });
124
+
125
+ });
126
+
127
+ function renumber(){
128
+
129
+ $('.no').each(function(){
130
+
131
+ var idx=$('.no').index(this);
132
+
133
+ $(this).text(idx+2);
134
+
135
+ $(this).next('input').attr('name','textarea['+(idx+1)+']');
136
+
137
+ });
138
+
139
+ };
140
+
141
+ function setButton(){
142
+
143
+ $('.addformbox').prop('disabled',$('.no').length>=max-1);
144
+
145
+ $('.deletformbox').prop('disabled',$('.no').length<=1);
146
+
147
+ }
148
+
149
+
150
+
151
+ </script>
152
+
153
+ <div>1</div>
154
+
155
+ <input type="text" name="textarea[0]" class="toiawase" placeholder="選択肢を入力">
156
+
157
+ <div class="box" data-formno="0">
158
+
159
+ <div id="input_pluralBox">
160
+
161
+ <div id="input_plural">
162
+
163
+ <div class="no">2</div>
164
+
165
+ <input type="text" name="textarea[1]" class="toiawase" placeholder="選択肢を入力">
166
+
167
+ <input type="button" value="-" class="deletformbox">
168
+
169
+ <input type="button" value="+" class="addformbox">
170
+
171
+ </div>
172
+
173
+ </div>
174
+
175
+ </div>
176
+
177
+ ```

1

chousei

2019/07/31 00:46

投稿

yambejp
yambejp

スコア114845

test CHANGED
@@ -40,7 +40,7 @@
40
40
 
41
41
  $(this).text(idx+2);
42
42
 
43
- $(this).next('input').attr('name','textarea['+(idx+2)+']');
43
+ $(this).next('input').attr('name','textarea['+(idx+1)+']');
44
44
 
45
45
 
46
46