前提・実現したいこと
javascriptについての質問です。
テーブル内の表計算を繰り返し処理や配列などを用いて簡潔に記述したいです。
テーブル1の処理は
単価(手入力)→ラジオボックス選択で掛け算(片道value=1,往復value=2)→合計値
テーブル2の処理は
単価(手入力)→選択リスト選択で掛け算(0~10)→合計値
→テーブル1と2の合計値
という手順でプログラムを実行したいです。
また、jQueryなどのプラグインは使わないで行いたいです。
発生している問題・エラーメッセージ
function multiply() { var element = document.getElementById( "form" ); var radioNodeList = element.which; var a = radioNodeList.value; var price = document.getElementsByName("price"); for (var i = 0; i < price.length; i++) { if (price[i].value == "") { } } }
該当のソースコード
<body> <form id="form" action="php-training-2-output.php" method="post"> <table> <tr> <th>単価</th> <th>利用区分</th> <th>合計</th> </tr> <tr> <td><input class="disable" maxlength='6' type="text" name="price"></td> <td><label><input type="radio" name="which" onChange="multiply()" value=1 checked>片道 <input type="radio" name="which" onChange="multiply()" value=2>往復</label></td> <td><input class="disable" maxlength='7' type="text" name="field"></td> </tr> <tr> <td><input class="disable" maxlength='6' type="text" name="price"></td> <td><label><input type="radio" name="which" onChange="multiply()" value=1 checked>片道 <input type="radio" name="which" onChange="multiply()" value=2>往復</label></td> <td><input class="disable" maxlength='7' type="text" name="field"></td> </tr> <tr> <td><input class="disable" maxlength='6' type="text" name="price"></td> <td><label><input type="radio" name="which" onChange="multiply()" value=1 checked>片道 <input type="radio" name="which" onChange="multiply()" value=2>往復</label></td> <td><input class="disable" maxlength='7' type="text" name="field"></td> </tr> <tr> <td align="right" colspan="3">合計</td> <td><input type="text" id="field_total" value="0"></td> </tr> </table> <table> <tr> <th>単価</th> <th>数量</th> <th>合計</th> </tr> <tr> <td><input class="disable" maxlength='6' type="text" name="price"></td> <td class="left"> <select name="price"> <?php $price=[0,1,2,3,4,5,6,7,8,9,10]; foreach($price as $item) { echo '<option value="', $item ,'">', $item ,'</option>';} ?> </select></td> <td><input class="disable" maxlength='7' type="text" name="field"></td> </tr> <tr> <td><input class="disable" maxlength='6' type="text" name="price"></td> <td class="left"> <select name="price"> <?php $price=[0,1,2,3,4,5,6,7,8,9,10]; foreach($price as $item) { echo '<option value="', $item ,'">', $item ,'</option>';} ?> </select></td> <td><input class="disable" maxlength='7' type="text" name="field"></td> </tr> <tr> <td><input class="disable" maxlength='6' type="text" name="price"></td> <td class="left"> <select name="price"> <?php $price=[0,1,2,3,4,5,6,7,8,9,10]; foreach($price as $item) { echo '<option value="', $item ,'">', $item ,'</option>';} ?> </select></td> <td><input class="disable" maxlength='7' type="text" name="field"></td> </tr> <tr> <td align="right" colspan="3">合計</td> <td><input type="text" id="field_total" value="0"></td> </tr> </table> <label>総合計</label> <input type="text" name="allTotal"> </body>
試したこと
javascript初心者のためどういった記述をすればいいのか全くわかりません。
一応上記のような記述をしてみてデバッカーで確認して、
値は取れているような気はするのですが、繰り返した処理をどうやって各テーブルの行
ごとに値を割り当て、各行の合計値に算出するのかわかりません。
また、ラジオボックスも行ごとに選択したいのですが、二行目のラジオボックスを選択すると
一行目のラジオボックスが未選択状態になるなどわからないことだらけです。
どうか解決方法をご教示お願い致します。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー