フォームへ同ページ内の値を自動入力する方法
ページ内上部で入力した値を、自動でフォームに同期させる方法をご存知の方が、いらっしゃいましたら、ご教示いただけないでしょうか?
(1)ユーザーが金額シュミレーター(カスタムコードで作成。コードは以下。)に任意のproduct-quantityを入力
(2)product-quantityの値が指定のStudio標準フォームパーツに常に入力される
(3)ユーザーはStudio標準のフォーム送信ボタンで、フォームをSendできる
以下、Chatgptに書いてもらった金額シュミレーターのコードです。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>商品購入ページ</title>
<style>
body {
font-family: 'Inter', sans-serif;
background-color: #ffffff;
color: #4d4d4d; /* 文字色を #4d4d4d に設定 */
}
table {
width: 100%;
border-collapse: collapse;
margin: 0 auto;
color: #4d4d4d; /* テーブル内の文字色を #4d4d4d に設定 */
}
table, th, td {
border: 1px solid #4d4d4d; /* テーブルの線の色を #4d4d4d に設定 */
}
th, td {
padding: 10px;
text-align: center;
}
/* 商品名の列を左寄せに設定 */
td:nth-child(2) {
text-align: left;
}
/* 横幅の割合を指定 */
th:nth-child(1), td:nth-child(1) { width: 20%; }
th:nth-child(2), td:nth-child(2) { width: 40%; }
th:nth-child(3), td:nth-child(3) { width: 10%; }
th:nth-child(4), td:nth-child(4) { width: 10%; }
th:nth-child(5), td:nth-child(5) { width: 10%; }
.total-price {
margin-top: 20px;
font-weight: bold;
text-align: right;
width: 100%;
margin-left: auto;
margin-right: auto;
color: #4d4d4d; /* 合計金額の文字色を #4d4d4d に設定 */
}
</style>
</head>
<body>
<!-- 商品リスト -->
<table id="product-table">
<thead>
<tr>
<th>商品画像</th>
<th>商品名</th>
<th>価格(税込)</th>
<th>数量</th>
<th>小計</th>
</tr>
</thead>
<tbody>
<!-- 商品1 -->
<tr>
<td><img src="https://github.com/Kaito-arkhome/blocky/blob/main/%E5%95%86%E5%93%811.png?raw=true" alt="Campaign10" width="200"></td>
<td>
10mセット<br>
※バラ購入より約10%お得にお求めいただけます。<br>
※100社を越える業者様より登録をいただいております関係で、1社様3セットまでとさせていただきます。
</td>
<td class="product-price" data-price="175000">¥175,000</td>
<td><input type="number" class="product-quantity" value="0" min="0" max="3"></td>
<td class="product-subtotal">¥0</td>
</tr>
<!-- 商品2 -->
<tr>
<td><img src="https://github.com/Kaito-arkhome/blocky/blob/main/%E5%95%86%E5%93%812.png?raw=true" alt="Campaign20" width="200"></td>
<td>
20mセット<br>
※バラ購入より約20%お得にお求めいただけます。<br>
※100社を越える業者様より登録をいただいております関係で、1社様3セットまでとさせていただきます。
</td>
<td class="product-price" data-price="311000">¥311,000</td>
<td><input type="number" class="product-quantity" value="0" min="0" max="3"></td>
<td class="product-subtotal">¥0</td>
</tr>
<!-- 商品3 -->
<tr>
<td><img src="https://github.com/Kaito-arkhome/blocky/blob/main/%E5%95%86%E5%93%813.png?raw=true" alt="CM-800" width="200"></td>
<td>CM-800(バラ購入)<br> 寸法:厚み120mm, 高さ450mm, 幅800mm
</td>
<td class="product-price" data-price="3480">¥3,480</td>
<td><input type="number" class="product-quantity" value="0" min="0" max="300"></td>
<td class="product-subtotal">¥0</td>
</tr>
<!-- 商品4 -->
<tr>
<td><img src="https://github.com/Kaito-arkhome/blocky/blob/main/%E5%95%86%E5%93%814.png?raw=true" alt="CE-800-M" width="200"></td>
<td>CE-800-M(バラ購入)<br> 寸法:厚み120mm, 高さ450mm, 幅800mm
</td>
<td class="product-price" data-price="3480">¥3,480</td>
<td><input type="number" class="product-quantity" value="0" min="0" max="100"></td>
<td class="product-subtotal">¥0</td>
</tr>
<!-- 商品5 -->
<tr>
<td><img src="https://github.com/Kaito-arkhome/blocky/blob/main/%E5%95%86%E5%93%815.png?raw=true" alt="CE-400-M" width="200"></td>
<td>CE-400-M(バラ購入)<br> 寸法:厚み120mm, 高さ450mm, 幅400mm
</td>
<td class="product-price" data-price="1980">¥1,980</td>
<td><input type="number" class="product-quantity" value="0" min="0" max="100"></td>
<td class="product-subtotal">¥0</td>
</tr>
<!-- 商品6 -->
<tr>
<td><img src="https://github.com/Kaito-arkhome/blocky/blob/main/%E5%95%86%E5%93%816.png?raw=true" alt="K-1000" width="200"></td>
<td>K-1000(バラ購入)<br> 寸法:高さ80mm, 幅1,000mm
</td>
<td class="product-price" data-price="980">¥980</td>
<td><input type="number" class="product-quantity" value="0" min="0" max="100"></td>
<td class="product-subtotal">¥0</td>
</tr>
</tbody>
</table>
<div class="total-price">合計(税込): ¥<span id="total-price">0</span></div>
<script>
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
function updateSubtotalAndTotal() {
const rows = document.querySelectorAll('#product-table tbody tr');
let total = 0;
rows.forEach(row => {
const price = parseInt(row.querySelector('.product-price').getAttribute('data-price'), 10);
const quantity = parseInt(row.querySelector('.product-quantity').value, 10);
const subtotal = price * quantity;
row.querySelector('.product-subtotal').textContent = `¥${formatNumber(subtotal)}`;
total += subtotal;
});
document.getElementById('total-price').textContent = formatNumber(total);
}
document.querySelectorAll('.product-quantity').forEach(input => {
input.addEventListener('input', updateSubtotalAndTotal);
});
// 初期計算
updateSubtotalAndTotal();
</script>
</body>
</html>