我想减去 2 个值,并立即使用 javascript 进行计算。我需要找到一种<select>
在 JS 中定位第二个 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>
我用 f1、f2、f3、f4 等和 t1、t2、t3、t4 等引用了 ID。我如何在 JS 中区分它们?
如果我只是将第一个的 ID<select>
引用为 $i,则下面的 JS 可以工作,我对 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();
}