私は自分用のサイトを作成しており、インスタント計算用の JavaScript を実装しようとしていますが、いくつかの問題があります。
タイピングを避ける方法はありますか
level_prices["3"]=3; level_prices["4"]=4;
... 90 まで: つまり、PHP コードで行ったように、
create
この値を 90 までにする方法はありますか?なぜこれが戻ってくるの
$Undefined
ですか?HTMLフォームから値を取得していないと思われますが、コードは正しいようです...
これが私のコードです:
関連する HTML/PHP:
<form action="" id="priceCalcForm" onsubmit="return false;">
<table class="table">
<tbody>
<tr> <!-- first row, with css -->
<td style="width:50%;">
<select name="fromlevel" id="fromlevel" onchange="calculateTotal()" style="width:100%; text-align:center; font-weight:bold;">
<option value="none" name="none">0</option>
<?php
$i = 1;
while ($i < 91) {
echo '
<option value=' . $i . ' name=' . $i . '>' . $i . '</option>';
$i++;
}
?>
</select>
</td>
</tr>
</tbody>
</table>
</form>
含まれる JS:
var level_prices= new Array();
level_prices["None"]=0;
level_prices["1"]=1;
level_prices["2"]=2;
function getLevelPrice()
{
var levelSetPrice=0;
//Get a reference to the form id="cakeform"
var theForm = document.forms["priceCalcForm"];
//Get a reference to the select id="filling"
var selectedLevel = theForm.elements["fromlevel"];
//set cakeFilling Price equal to value user chose
//For example filling_prices["Lemon".value] would be equal to 5
levelSetPrice = level_prices[selectedLevel.value];
//finally we return cakeFillingPrice
return levelSetPrice;
}
function calculateTotal()
{
//Here we get the total price by calling our function
//Each function returns a number so by calling them we add the values they return together
var LevelPrice = getLevelPrice();
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';
}
私は自分自身を PHP でまともだと思っていますが、JS を使用するのは初めてです。