私は2つの値を減算し、javascriptで即座に計算を行いたい. <select>
オプションをエコーするため、JS で2 番目の ID をターゲットにする方法を見つける必要があります。次のようにします。
<tr> <!-- first row, with css -->
<td style="width:50%;">From</td>
<td style="width:50%;">
<select name="fromlevel" id="fromlevel" style="width:100%; text-align:center; font-weight:bold;">
<?php
$i = 1;
while ($i < 91) {
echo '
<option value=f' . $i . ' name=f' . $i . '>' . $i . '</option>';
$i++;
}
?>
</select>
</td>
</tr>
<tr> <!-- second row, with css -->
<td>To</td>
<td>
<select name="tolevel" id="tolevel" style="width:100%; text-align:center; font-weight:bold;">
<?php
$i = 1;
while ($i < 91) {
echo '
<option value=t' . $i . ' name=t' . $i . '>' . $i . '</option>';
$i++;
}
?>
</select>
</td>
</tr>
ID を f1、f2、f3、f4 など、および t1、t2、t3、t4 などで参照しました。JS でそれらを区別するにはどうすればよいですか?
以下の JS は、最初の ID を<select>
$i として参照するだけで機能します。JS が非常に苦手で、その参照を f$i にする方法がわかりません。
var level_current_prices = {};
for(var i = 1; i <= 90; i++ ) {
level_current_prices[i] = i;
}
function getCurrentLevel() { // current level, from
var levelSetPrice=0;
var theForm = document.forms["priceCalcForm"];
var selectedLevel = theForm.elements["fromlevel"];
levelSetPrice = level_current_prices[selectedLevel.value];
return levelSetPrice;
}
function calculateTotal() {
var LevelPrice = getCurrentLevel();
var divobj = document.getElementById('totalPrice');
divobj.style.display='block';
divobj.innerHTML = "Total Price For the Leveling $"+LevelPrice;
}
function hideTotal() {
var divobj = document.getElementById('totalPrice');
divobj.style.display='none';
}
document.forms['priceCalcForm'].fromlevel.onchange = function () {
calculateTotal();
}