teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

7

修正

2020/10/09 16:27

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -22,250 +22,4 @@
22
22
 
23
23
  ```JavaScript
24
24
 
25
- function calc(){
26
- // railsのビューファイルから、各入力・出力フォームのIDを取得している
27
- const total_payment = document.getElementById("total-payment");
28
- const total_people = document.getElementById("total-people");
29
-
30
- const group_people_one = document.getElementById("group-people-one");
31
- const group_price_one = document.getElementById("group-price-one");
32
- const group_ratio_one = document.getElementById("group-ratio-one");
33
-
34
- const group_people_two = document.getElementById("group-people-two");
35
- const group_price_two = document.getElementById("group-price-two");
36
- const group_ratio_two = document.getElementById("group-ratio-two");
37
-
38
- const group_people_three = document.getElementById("group-people-three");
39
- const group_price_three = document.getElementById("group-price-three");
40
- const group_ratio_three = document.getElementById("group-ratio-three");
41
-
42
- const indivisible_number = document.getElementById('indivisible-number');
43
-
44
- // グループ2
45
- group_ratio_two.addEventListener('keyup', () => {
46
- const people_value = total_people.value;
47
- const people_two_value = group_people_two.value;
48
-
49
- const people_three_value = group_people_three.value;
50
-
51
- const people = people_value - people_two_value - people_three_value;
52
- group_people_one.textContent = people;
53
-
54
- const payment_value = total_payment.value;
55
- let price_two_value = group_price_two.value;
56
- const price_three_value = group_price_three.value;
57
-
58
- const payment = (payment_value - ((price_two_value * people_two_value) + (price_three_value * people_three_value))) / people;
59
- const payment_cell = Math.ceil(payment/100)*100;
60
- group_price_one.textContent = payment_cell ;
61
-
62
-
63
- const ratio_two_value = group_ratio_two.value;
64
- const ratio_two = ((ratio_two_value / 100) * payment_value) / people_two_value
65
- const ratio_cell_two = Math.ceil(ratio_two/100)*100;
66
- group_price_two.value = ratio_cell_two
67
-
68
- const ratio_three_value = group_ratio_three.value;
69
- group_ratio_one.textContent = 100 - ratio_three_value - ratio_two_value;
70
-
71
-
72
- formula = (payment_cell * people) + (price_two_value * people_two_value) + (price_three_value * people_three_value);
73
- indivisible_number.textContent = (formula - payment_value);
74
- });
75
- // グループ2
76
-
77
- // グループ3
78
- group_ratio_three.addEventListener('keyup', () => {
79
- const people_value = total_people.value;
80
- const people_two_value = group_people_two.value;
81
-
82
- const people_three_value = group_people_three.value;
83
-
84
- const people = people_value - people_two_value - people_three_value;
85
- group_people_one.textContent = people;
86
-
87
- const payment_value = total_payment.value;
88
- const price_two_value = group_price_two.value;
89
- const price_three_value = group_price_three.value;
90
-
91
- const payment = (payment_value - ((price_two_value * people_two_value) + (price_three_value * people_three_value))) / people;
92
- const payment_cell = Math.ceil(payment/100)*100;
93
- group_price_one.textContent = payment_cell ;
94
-
95
- const ratio_three_value = group_ratio_three.value;
96
- const ratio_three = ((ratio_three_value / 100) * payment_value) / people_three_value
97
- const ratio_cell_three = Math.ceil(ratio_three/100)*100;
98
- group_price_three.value = ratio_cell_three
99
-
100
- const ratio_two_value = group_ratio_two.value;
101
- group_ratio_one.textContent = 100 - ratio_three_value - ratio_two_value;
102
-
103
- formula = (payment_cell * people) + (price_two_value * people_two_value) + (price_three_value * people_three_value);
104
- indivisible_number.textContent = (formula - payment_value);
105
- });
106
- // グループ3
107
- }
108
- window.addEventListener("load", calc);
109
- ```
110
-
111
- ```HTML
112
-
113
- <!DOCTYPE html>
114
- <html>
115
- <head>
116
- <title>SplitTheBill</title>
117
- <meta name="csrf-param" content="authenticity_token" />
118
- <meta name="csrf-token" content="gfsQI82k3BKLmAf7cBuuMNpe7vaTiaos8JvbM3hRXcNsxdaSupVkm70AJA+Jf3v2Bs+xl4F0d4roBmTtPrNkkw==" />
119
-
120
-
121
- <link rel="stylesheet" media="all" href="/assets/calculations.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" data-turbolinks-track="reload" />
122
- <link rel="stylesheet" media="all" href="/assets/calculations/index.html.self-3d3c25769ab198a66e397d7d08478d8fe0bff2e27c6bf7b28e53cab28d7cba07.css?body=1" data-turbolinks-track="reload" />
123
- <link rel="stylesheet" media="all" href="/assets/application.self-f0d704deea029cf000697e2c0181ec173a1b474645466ed843eb5ee7bb215794.css?body=1" data-turbolinks-track="reload" />
124
- <script src="/packs/js/application-cedbf28281aaf375c93f.js" data-turbolinks-track="reload"></script>
125
- </head>
126
-
127
- <body>
128
- <div class="calc-contents">
129
- <header class="calc-header">
130
- </header>
131
- <div class="calc-main">
132
- <form action="/" accept-charset="UTF-8" method="post"><input type="hidden" name="authenticity_token" value="3ynDmPLH1QMQiOCAvMa0M4v1Se71JYvnAHIcRrwYcAjjEsRrUxxVypaw0o6pWcp7qd7MVavoDXPlmJNyjyUm1w==" />
133
- <div class="calc-title-one">情報を入力</div>
134
- <div class="total">
135
- <div class="total-payment">
136
- <span>総支払額</span>
137
- <span class="indispensable">※</span>
138
- <input class="total-people" id="total-payment" placeholder="例)50000" type="text" name="hoge" />円
139
- </div>
140
- <div class="total-people">
141
- <span>総人数</span>
142
- <span class="indispensable">※</span>
143
- <input class="total-people" id="total-people" placeholder="例)5" type="text" name="hoge" />人
144
- </div>
145
- </div>
146
- <div class="calc-title-two">計算を入力</div>
147
- <div class="calc-description">グループ2以降から数値を入力してください。<span class="indispensable-description">※は必須項目です。</span></div>
148
- <div class="calc-description">比率か一人当たりの金額どちらか一方を記入すれば、もう一方が自動で表示されます。</div>
149
- <div class="description_one">グループ1には、総額・総人数からグループ2以降の値を引いた数値が表示されます。</div>
150
- <div class ="description">一人あたりの支払額は十の位で切り上げを行うため、あまりが生じます。</div>
151
- <div class="description_two">比率は小数点第二位で切り捨てを行なっています。</div>
152
-
153
-
154
-
155
- <div class="group">
156
- <div class="group-text">
157
- <span class="group-name"> グループ名</span>
158
- </div>
159
-
160
- <div class="group">
161
- <div class="group-text">
162
- <span>人数</span>
163
- </div>
164
- </div>
165
-
166
- <div class="group">
167
- <div class="group-text">
168
- <span>比率</span>
169
- </div>
170
- </div>
171
-
172
- <div class="group">
173
- <div class="group-text">
174
- <span>一人あたり</span>
175
- </div>
176
- </div>
177
- </div>
178
-
179
-
180
- <div class="group">
181
- <div class="group-text">
182
- <span class="group-name"></span>
183
- <input class="group-input-name" id="group-name-one" placeholder="例)グループ1" type="text" name="hoge" />
184
- </div>
185
-
186
- <div class="group">
187
- <div class="group-text-one">
188
- <span id='group-people-one'></span>
189
- <span class = "unit">人</span>
190
- </div>
191
- </div>
192
-
193
- <div class="group">
194
- <div class="group-text-one">
195
- <span id='group-ratio-one'></span>
196
- <span class = "unit"> %</span>
197
- </div>
198
- </div>
199
-
200
- <div class="group">
201
- <div class="group-text-one">
202
- <span id='group-price-one'></span>
203
- <span class = "unit"> 円</span>
204
- </div>
205
- </div>
206
- </div>
207
-
208
-
209
- <div class="group">
210
- <div class="group-text">
211
-
212
- <input class="group-input-name" id="group-name-two" placeholder="例)グループ2" type="text" name="hoge" />
213
- </div>
214
-
215
- <div class="group-text">
216
-
217
- <span class="indispensable">※</span>
218
- <input class="group-input" id="group-people-two" placeholder="例)1" type="text" name="hoge" />人
219
- </div>
220
-
221
- <div class="group-text">
222
-
223
- <span class="indispensable">※</span>
224
- <input class="group-input" id="group-ratio-two" placeholder="例)10" type="text" name="hoge" />%
225
- </div>
226
-
227
- <div class="group-text">
228
-
229
- <span class="indispensable">※</span>
230
- <input class="group-input" id="group-price-two" placeholder="例)10000" type="text" name="hoge" />円
231
- </div>
232
- </div>
233
-
234
- <div class="group">
235
- <div class="group-text">
236
-
237
- <input class="group-input-name" id="group-name-three" placeholder="例)グループ3" type="text" name="hoge" />
238
- </div>
239
-
240
- <div class="group-text">
241
-
242
- <span class="indispensable">※</span>
243
- <input class="group-input" id="group-people-three" placeholder="例)1" type="text" name="hoge" />人
244
- </div>
245
-
246
- <div class="group-text">
247
-
248
- <span class="indispensable">※</span>
249
- <input class="group-input" id="group-ratio-three" placeholder="例)10" type="text" name="hoge" />%
250
- </div>
251
-
252
-
253
- <div class="group-text">
254
-
255
- <span class="indispensable">※</span>
256
- <input class="group-input" id="group-price-three" placeholder="例)10000" type="text" name="hoge" />円
257
- </div>
258
- </div>
259
-
260
- <div class="group-invisible">
261
- <div class="indivisible-number">
262
- <span>あまり</span>
263
- <span id='indivisible-number'></span>円
264
- </div>
265
- </div>
266
-
267
- </form></div>
268
- </body>
269
- </html>
270
-
271
25
  ```

6

若葉マークの追加

2020/10/09 16:27

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -3,6 +3,7 @@
3
3
  ruby on rails上でJavaScriptを使い、
4
4
  一つのフォームに値を入力すると、他のフォームにも数字が出力される仕組みを実現したいです。
5
5
 
6
+
6
7
  ### 発生している問題・エラーメッセージ
7
8
  一度目は思った通りの処理が動きます。
8
9
  ただ、**何度かフォームの数字を打ち直していると、出力される数字が変わってしまいます。**

5

HTMLの修正

2020/10/09 15:27

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,6 @@
10
10
  この時、数字は2通り表示されます。一方は正しい値、もう一方は正しくない値ですが、正しくない方値も何度か繰り返しても同じ数字が出ます。
11
11
 
12
12
  **今回数字が変わってしまうのは下記コードのprice_two_valueの部分です。**
13
- (ちなみにブラウザ上で数字に異変が起きるのはgroup_price_oneの方でした。console.logを用いてgroup_price_oneを出力するための計算式に含まれている数を一つ一つ確認したところ、price_two_valueが見た目は変わらないのに中身の数字が変化してしまっていると分かりました。)
14
13
 
15
14
  **例えば入力フォームに「30」と入力したら、当然ですが、その30をもとに計算出力したいです。
16
15
  しかしなぜか、たびたび「3」に対して計算が行われているようです。その時に誤った表示がされています。**
@@ -18,37 +17,8 @@
18
17
  その「3」は、おそらく30を入力する途中、消す途中の「3」です。なぜこのような中途半端なタイミングで発火してしまうのか分かりません。
19
18
 
20
19
 
21
- 記述やイベント発火させようとしているフォームが多いため、
22
- 今回はそのエラーが起きる部分のイベント発火の部分のみ記載します。
23
-
24
- その他のフォーム・イベント発火にも関連して問題がありそうな場合、追記しますので教えていただけますでしょうか。
25
-
26
- どうぞよろしくお願いいたします。
27
-
28
-
29
-
30
20
  ### 該当のソースコード
31
- 前提として、今回作成中のアプリは割勘計算機能です。
32
21
 
33
- total_payment(総支払額)とtotal_people(総人数)は前提条件のようなもので、このフォームに入力された値をもとに、様々な計算を行います。
34
-
35
- そのほかにグループを三つ用意しています。
36
-
37
- group_people_two、group_price_two、group_ratio_two
38
- 上記はグループ2の、左から順に人数、料金、比率の値を入れていくフォームです。
39
-
40
- group_people_three、group_price_three、group_ratio_three
41
- 上記はグループ3です。
42
-
43
- group_people_one、group_price_one、group_ratio_one
44
- 上記はグループ1ですが、グループ1のみ入力フォームではなく出力するために用意しています。
45
- 前提条件からグループ2と3の数値を引いて、グループ1に表示させようとしています。
46
-
47
- price(料金)とratio(比率)に関しては、一方に値を入れた時、もう一方の値が自動で表示されるようにしています。
48
- この部分で、今回の問題が発生しています。
49
-
50
- indivisible_numberは余った値を出力しようとしています。
51
-
52
22
  ```JavaScript
53
23
 
54
24
  function calc(){
@@ -138,119 +108,153 @@
138
108
  ```
139
109
 
140
110
  ```HTML
111
+
112
+ <!DOCTYPE html>
113
+ <html>
114
+ <head>
115
+ <title>SplitTheBill</title>
116
+ <meta name="csrf-param" content="authenticity_token" />
117
+ <meta name="csrf-token" content="gfsQI82k3BKLmAf7cBuuMNpe7vaTiaos8JvbM3hRXcNsxdaSupVkm70AJA+Jf3v2Bs+xl4F0d4roBmTtPrNkkw==" />
118
+
119
+
120
+ <link rel="stylesheet" media="all" href="/assets/calculations.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" data-turbolinks-track="reload" />
121
+ <link rel="stylesheet" media="all" href="/assets/calculations/index.html.self-3d3c25769ab198a66e397d7d08478d8fe0bff2e27c6bf7b28e53cab28d7cba07.css?body=1" data-turbolinks-track="reload" />
122
+ <link rel="stylesheet" media="all" href="/assets/application.self-f0d704deea029cf000697e2c0181ec173a1b474645466ed843eb5ee7bb215794.css?body=1" data-turbolinks-track="reload" />
123
+ <script src="/packs/js/application-cedbf28281aaf375c93f.js" data-turbolinks-track="reload"></script>
124
+ </head>
125
+
126
+ <body>
141
- <div class="calc-contents">
127
+ <div class="calc-contents">
142
128
  <header class="calc-header">
143
129
  </header>
144
130
  <div class="calc-main">
145
- <%= form_with local: true do |f| %>
131
+ <form action="/" accept-charset="UTF-8" method="post"><input type="hidden" name="authenticity_token" value="3ynDmPLH1QMQiOCAvMa0M4v1Se71JYvnAHIcRrwYcAjjEsRrUxxVypaw0o6pWcp7qd7MVavoDXPlmJNyjyUm1w==" />
146
132
  <div class="calc-title-one">情報を入力</div>
147
133
  <div class="total">
148
134
  <div class="total-payment">
149
135
  <span>総支払額</span>
150
136
  <span class="indispensable">※</span>
151
- <%= f.text_field :hoge, class:"total-people", id:"total-payment", placeholder:"例)50000" %>円
137
+ <input class="total-people" id="total-payment" placeholder="例)50000" type="text" name="hoge" />円
152
138
  </div>
153
139
  <div class="total-people">
154
140
  <span>総人数</span>
155
141
  <span class="indispensable">※</span>
156
- <%= f.text_field :hoge, class:"total-people", id:"total-people", placeholder:"例)5" %>人
142
+ <input class="total-people" id="total-people" placeholder="例)5" type="text" name="hoge" />人
157
143
  </div>
158
144
  </div>
159
145
  <div class="calc-title-two">計算を入力</div>
160
146
  <div class="calc-description">グループ2以降から数値を入力してください。<span class="indispensable-description">※は必須項目です。</span></div>
161
147
  <div class="calc-description">比率か一人当たりの金額どちらか一方を記入すれば、もう一方が自動で表示されます。</div>
162
148
  <div class="description_one">グループ1には、総額・総人数からグループ2以降の値を引いた数値が表示されます。</div>
163
- <div class ="description_two">10の位で切り上げを行うため、あまりが生じます。</div>
149
+ <div class ="description">一人あたり支払額は十の位で切り上げを行うため、あまりが生じます。</div>
164
- <div class="description_one">比率は小数点二位で切り捨てを行なっています。</div>
150
+ <div class="description_two">比率は小数点二位で切り捨てを行なっています。</div>
165
151
 
166
152
 
167
- <%# 販売価格 %>
153
+
168
- <%# グループ1 %>
169
-
170
154
  <div class="group">
171
155
  <div class="group-text">
172
156
  <span class="group-name"> グループ名</span>
173
- <%= f.text_field :hoge, class:"group-input-name", id:"group-name-one", placeholder:"例)グループ1" %>
174
157
  </div>
175
158
 
176
159
  <div class="group">
177
- <div class="group-text-one">
160
+ <div class="group-text">
178
161
  <span>人数</span>
179
- <span id='group-people-one'></span>人
180
162
  </div>
181
163
  </div>
182
164
 
183
165
  <div class="group">
184
- <div class="group-text-one">
166
+ <div class="group-text">
185
167
  <span>比率</span>
186
- <span id='group-ratio-one'></span>%
187
168
  </div>
188
169
  </div>
189
170
 
190
171
  <div class="group">
191
- <div class="group-text-one">
172
+ <div class="group-text">
192
173
  <span>一人あたり</span>
193
- <span id='group-price-one'></span>円
194
174
  </div>
195
175
  </div>
196
176
  </div>
197
177
 
178
+
179
+ <div class="group">
180
+ <div class="group-text">
181
+ <span class="group-name"></span>
182
+ <input class="group-input-name" id="group-name-one" placeholder="例)グループ1" type="text" name="hoge" />
198
- <%# グループ1 %>
183
+ </div>
199
184
 
185
+ <div class="group">
186
+ <div class="group-text-one">
187
+ <span id='group-people-one'></span>
188
+ <span class = "unit">人</span>
200
- <%# グループ2 %>
189
+ </div>
190
+ </div>
191
+
192
+ <div class="group">
193
+ <div class="group-text-one">
194
+ <span id='group-ratio-one'></span>
195
+ <span class = "unit"> %</span>
196
+ </div>
197
+ </div>
198
+
199
+ <div class="group">
200
+ <div class="group-text-one">
201
+ <span id='group-price-one'></span>
202
+ <span class = "unit"> 円</span>
203
+ </div>
204
+ </div>
205
+ </div>
206
+
207
+
201
208
  <div class="group">
202
209
  <div class="group-text">
203
- <span>グループ名</span>
210
+
204
- <%= f.text_field :hoge, class:"group-input-name", id:"group-name-two", placeholder:"例)グループ2" %>
211
+ <input class="group-input-name" id="group-name-two" placeholder="例)グループ2" type="text" name="hoge" />
205
212
  </div>
206
213
 
207
214
  <div class="group-text">
208
- <span>人数</span>
215
+
209
216
  <span class="indispensable">※</span>
210
- <%= f.text_field :hoge, class:"group-input", id:"group-people-two", placeholder:"例)1" %>人
217
+ <input class="group-input" id="group-people-two" placeholder="例)1" type="text" name="hoge" />人
211
218
  </div>
212
219
 
213
220
  <div class="group-text">
214
- <span>比率</span>
221
+
215
222
  <span class="indispensable">※</span>
216
- <%= f.text_field :hoge, class:"group-input", id:"group-ratio-two" ,placeholder:"例)10" %>%
223
+ <input class="group-input" id="group-ratio-two" placeholder="例)10" type="text" name="hoge" />%
217
224
  </div>
218
225
 
219
226
  <div class="group-text">
220
- <span>一人あたり</span>
227
+
221
228
  <span class="indispensable">※</span>
222
- <%= f.text_field :hoge, class:"group-input", id:"group-price-two", placeholder:"例)10000" %>円
229
+ <input class="group-input" id="group-price-two" placeholder="例)10000" type="text" name="hoge" />円
223
230
  </div>
224
231
  </div>
225
- <%# グループ2 %>
226
232
 
227
- <%# グループ3 %>
228
233
  <div class="group">
229
234
  <div class="group-text">
230
- <span>グループ名</span>
235
+
231
- <%= f.text_field :hoge, class:"group-input-name", id:"group-name-three", placeholder:"例)グループ3" %>
236
+ <input class="group-input-name" id="group-name-three" placeholder="例)グループ3" type="text" name="hoge" />
232
237
  </div>
233
238
 
234
239
  <div class="group-text">
235
- <span>人数</span>
240
+
236
241
  <span class="indispensable">※</span>
237
- <%= f.text_field :hoge, class:"group-input", id:"group-people-three", placeholder:"例)1" %>人
242
+ <input class="group-input" id="group-people-three" placeholder="例)1" type="text" name="hoge" />人
238
243
  </div>
239
244
 
240
245
  <div class="group-text">
241
- <span>比率</span>
246
+
242
247
  <span class="indispensable">※</span>
243
- <%= f.text_field :hoge, class:"group-input", id:"group-ratio-three", placeholder:"例)10" %>%
248
+ <input class="group-input" id="group-ratio-three" placeholder="例)10" type="text" name="hoge" />%
244
249
  </div>
245
250
 
246
251
 
247
252
  <div class="group-text">
248
- <span>一人あたり</span>
253
+
249
254
  <span class="indispensable">※</span>
250
- <%= f.text_field :hoge, class:"group-input", id:"group-price-three", placeholder:"例)10000" %>円
255
+ <input class="group-input" id="group-price-three" placeholder="例)10000" type="text" name="hoge" />円
251
256
  </div>
252
257
  </div>
253
- <%# グループ3 %>
254
258
 
255
259
  <div class="group-invisible">
256
260
  <div class="indivisible-number">
@@ -258,8 +262,9 @@
258
262
  <span id='indivisible-number'></span>円
259
263
  </div>
260
264
  </div>
261
- <%# /販売価格 %>
262
265
 
263
- <% end %>
266
+ </form></div>
264
- </div>
267
+ </body>
268
+ </html>
269
+
265
270
  ```

4

誤って削除した部分の修正

2020/10/09 05:50

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -51,6 +51,25 @@
51
51
 
52
52
  ```JavaScript
53
53
 
54
+ function calc(){
55
+ // railsのビューファイルから、各入力・出力フォームのIDを取得している
56
+ const total_payment = document.getElementById("total-payment");
57
+ const total_people = document.getElementById("total-people");
58
+
59
+ const group_people_one = document.getElementById("group-people-one");
60
+ const group_price_one = document.getElementById("group-price-one");
61
+ const group_ratio_one = document.getElementById("group-ratio-one");
62
+
63
+ const group_people_two = document.getElementById("group-people-two");
64
+ const group_price_two = document.getElementById("group-price-two");
65
+ const group_ratio_two = document.getElementById("group-ratio-two");
66
+
67
+ const group_people_three = document.getElementById("group-people-three");
68
+ const group_price_three = document.getElementById("group-price-three");
69
+ const group_ratio_three = document.getElementById("group-ratio-three");
70
+
71
+ const indivisible_number = document.getElementById('indivisible-number');
72
+
54
73
  // グループ2
55
74
  group_ratio_two.addEventListener('keyup', () => {
56
75
  const people_value = total_people.value;
@@ -114,6 +133,8 @@
114
133
  indivisible_number.textContent = (formula - payment_value);
115
134
  });
116
135
  // グループ3
136
+ }
137
+ window.addEventListener("load", calc);
117
138
  ```
118
139
 
119
140
  ```HTML

3

グループ3のイベント発火の記述を追加

2020/10/09 03:33

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -51,25 +51,8 @@
51
51
 
52
52
  ```JavaScript
53
53
 
54
- const total_payment = document.getElementById("total-payment");
55
- const total_people = document.getElementById("total-people");
56
-
57
- const group_people_one = document.getElementById("group-people-one");
58
- const group_price_one = document.getElementById("group-price-one");
59
- const group_ratio_one = document.getElementById("group-ratio-one");
60
-
61
- const group_people_two = document.getElementById("group-people-two");
62
- const group_price_two = document.getElementById("group-price-two");
63
- const group_ratio_two = document.getElementById("group-ratio-two");
64
-
65
- const group_people_three = document.getElementById("group-people-three");
66
- const group_price_three = document.getElementById("group-price-three");
67
- const group_ratio_three = document.getElementById("group-ratio-three");
68
-
69
- const indivisible_number = document.getElementById('indivisible-number');
54
+ // グループ2
70
-
71
-
72
- group_ratio_two.addEventListener('keyup', () => {
55
+ group_ratio_two.addEventListener('keyup', () => {
73
56
  const people_value = total_people.value;
74
57
  const people_two_value = group_people_two.value;
75
58
 
@@ -99,6 +82,38 @@
99
82
  formula = (payment_cell * people) + (price_two_value * people_two_value) + (price_three_value * people_three_value);
100
83
  indivisible_number.textContent = (formula - payment_value);
101
84
  });
85
+ // グループ2
86
+
87
+ // グループ3
88
+ group_ratio_three.addEventListener('keyup', () => {
89
+ const people_value = total_people.value;
90
+ const people_two_value = group_people_two.value;
91
+
92
+ const people_three_value = group_people_three.value;
93
+
94
+ const people = people_value - people_two_value - people_three_value;
95
+ group_people_one.textContent = people;
96
+
97
+ const payment_value = total_payment.value;
98
+ const price_two_value = group_price_two.value;
99
+ const price_three_value = group_price_three.value;
100
+
101
+ const payment = (payment_value - ((price_two_value * people_two_value) + (price_three_value * people_three_value))) / people;
102
+ const payment_cell = Math.ceil(payment/100)*100;
103
+ group_price_one.textContent = payment_cell ;
104
+
105
+ const ratio_three_value = group_ratio_three.value;
106
+ const ratio_three = ((ratio_three_value / 100) * payment_value) / people_three_value
107
+ const ratio_cell_three = Math.ceil(ratio_three/100)*100;
108
+ group_price_three.value = ratio_cell_three
109
+
110
+ const ratio_two_value = group_ratio_two.value;
111
+ group_ratio_one.textContent = 100 - ratio_three_value - ratio_two_value;
112
+
113
+ formula = (payment_cell * people) + (price_two_value * people_two_value) + (price_three_value * people_three_value);
114
+ indivisible_number.textContent = (formula - payment_value);
115
+ });
116
+ // グループ3
102
117
  ```
103
118
 
104
119
  ```HTML

2

HTMLの修正

2020/10/09 03:31

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -102,7 +102,36 @@
102
102
  ```
103
103
 
104
104
  ```HTML
105
+ <div class="calc-contents">
106
+ <header class="calc-header">
107
+ </header>
108
+ <div class="calc-main">
109
+ <%= form_with local: true do |f| %>
110
+ <div class="calc-title-one">情報を入力</div>
111
+ <div class="total">
112
+ <div class="total-payment">
113
+ <span>総支払額</span>
114
+ <span class="indispensable">※</span>
115
+ <%= f.text_field :hoge, class:"total-people", id:"total-payment", placeholder:"例)50000" %>円
116
+ </div>
117
+ <div class="total-people">
118
+ <span>総人数</span>
119
+ <span class="indispensable">※</span>
120
+ <%= f.text_field :hoge, class:"total-people", id:"total-people", placeholder:"例)5" %>人
121
+ </div>
122
+ </div>
123
+ <div class="calc-title-two">計算を入力</div>
124
+ <div class="calc-description">グループ2以降から数値を入力してください。<span class="indispensable-description">※は必須項目です。</span></div>
125
+ <div class="calc-description">比率か一人当たりの金額どちらか一方を記入すれば、もう一方が自動で表示されます。</div>
126
+ <div class="description_one">グループ1には、総額・総人数からグループ2以降の値を引いた数値が表示されます。</div>
127
+ <div class ="description_two">10の位で切り上げを行うため、あまりが生じます。</div>
128
+ <div class="description_one">比率は小数点二位で切り捨てを行なっています。</div>
129
+
130
+
131
+ <%# 販売価格 %>
132
+ <%# グループ1 %>
133
+
105
- <div class="group">
134
+ <div class="group">
106
135
  <div class="group-text">
107
136
  <span class="group-name"> グループ名</span>
108
137
  <%= f.text_field :hoge, class:"group-input-name", id:"group-name-one", placeholder:"例)グループ1" %>
@@ -194,4 +223,7 @@
194
223
  </div>
195
224
  </div>
196
225
  <%# /販売価格 %>
226
+
227
+ <% end %>
228
+ </div>
197
229
  ```

1

HTMLの追加

2020/10/09 03:27

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -99,4 +99,99 @@
99
99
  formula = (payment_cell * people) + (price_two_value * people_two_value) + (price_three_value * people_three_value);
100
100
  indivisible_number.textContent = (formula - payment_value);
101
101
  });
102
+ ```
103
+
104
+ ```HTML
105
+ <div class="group">
106
+ <div class="group-text">
107
+ <span class="group-name"> グループ名</span>
108
+ <%= f.text_field :hoge, class:"group-input-name", id:"group-name-one", placeholder:"例)グループ1" %>
109
+ </div>
110
+
111
+ <div class="group">
112
+ <div class="group-text-one">
113
+ <span>人数</span>
114
+ <span id='group-people-one'></span>人
115
+ </div>
116
+ </div>
117
+
118
+ <div class="group">
119
+ <div class="group-text-one">
120
+ <span>比率</span>
121
+ <span id='group-ratio-one'></span>%
122
+ </div>
123
+ </div>
124
+
125
+ <div class="group">
126
+ <div class="group-text-one">
127
+ <span>一人あたり</span>
128
+ <span id='group-price-one'></span>円
129
+ </div>
130
+ </div>
131
+ </div>
132
+
133
+ <%# グループ1 %>
134
+
135
+ <%# グループ2 %>
136
+ <div class="group">
137
+ <div class="group-text">
138
+ <span>グループ名</span>
139
+ <%= f.text_field :hoge, class:"group-input-name", id:"group-name-two", placeholder:"例)グループ2" %>
140
+ </div>
141
+
142
+ <div class="group-text">
143
+ <span>人数</span>
144
+ <span class="indispensable">※</span>
145
+ <%= f.text_field :hoge, class:"group-input", id:"group-people-two", placeholder:"例)1" %>人
146
+ </div>
147
+
148
+ <div class="group-text">
149
+ <span>比率</span>
150
+ <span class="indispensable">※</span>
151
+ <%= f.text_field :hoge, class:"group-input", id:"group-ratio-two" ,placeholder:"例)10" %>%
152
+ </div>
153
+
154
+ <div class="group-text">
155
+ <span>一人あたり</span>
156
+ <span class="indispensable">※</span>
157
+ <%= f.text_field :hoge, class:"group-input", id:"group-price-two", placeholder:"例)10000" %>円
158
+ </div>
159
+ </div>
160
+ <%# グループ2 %>
161
+
162
+ <%# グループ3 %>
163
+ <div class="group">
164
+ <div class="group-text">
165
+ <span>グループ名</span>
166
+ <%= f.text_field :hoge, class:"group-input-name", id:"group-name-three", placeholder:"例)グループ3" %>
167
+ </div>
168
+
169
+ <div class="group-text">
170
+ <span>人数</span>
171
+ <span class="indispensable">※</span>
172
+ <%= f.text_field :hoge, class:"group-input", id:"group-people-three", placeholder:"例)1" %>人
173
+ </div>
174
+
175
+ <div class="group-text">
176
+ <span>比率</span>
177
+ <span class="indispensable">※</span>
178
+ <%= f.text_field :hoge, class:"group-input", id:"group-ratio-three", placeholder:"例)10" %>%
179
+ </div>
180
+
181
+
182
+ <div class="group-text">
183
+ <span>一人あたり</span>
184
+ <span class="indispensable">※</span>
185
+ <%= f.text_field :hoge, class:"group-input", id:"group-price-three", placeholder:"例)10000" %>円
186
+ </div>
187
+ </div>
188
+ <%# グループ3 %>
189
+
190
+ <div class="group-invisible">
191
+ <div class="indivisible-number">
192
+ <span>あまり</span>
193
+ <span id='indivisible-number'></span>円
194
+ </div>
195
+ </div>
196
+ <%# /販売価格 %>
102
197
  ```