我知道这类问题以前曾被问过,大多数情况下由于元素ID不同而已得到解决。但是,我已经将ID从元素复制并粘贴到JS代码中,即使在输入字段上使用value =“#”时,仍然会收到错误消息。任何想法都会有所帮助。
//Var
var numsessionsvar = parseInt(document.getElementById("numsessions").value);
var timeofdayvar = document.getElementById("timeofday").value;
var dayvar = document.getElementById("day").value;
var price = "";
//Pricing
function calculate() {
if (timeofday = "0700" || "0800") {
price = "25";
}
if (timeofday = "0900" || "1000" || "1100") {
price = "Normal Rates";
}
if (timeofday = "1200" || "1300") {
if (day = "Saturday") {
price = "20";
}
else {
price = "30";
}
}
if (timeofday = "1400" || "1500") {
price = "20";
}
if (timeofday = "1600" || "1700") {
if (day = "Saturday") {
price = "20";
}
else {
price = "Normal Rate";
}
}
if (timeofday = "1800") {
price = "Normal Rates";
}
if (timeofday = "1900") {
price = "30";
}
if (timeofday = "2000") {
price = "20";
}
//Output
var total = price * numsessions;
var output = "£" + total + ".00";
document.getElementById("output").value = output;
}
<body>
<input type="number" id="numsessions" min="1" max="5" value="1" />
<select id="day">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
</select>
<select id="timeofday">
<option value="0700">0700</option>
<option value="0800">0800</option>
<option value="0900">0900</option>
<option value="1000">1000</option>
<option value="1100">1100</option>
<option value="1200">1200</option>
<option value="1300">1300</option>
<option value="1400">1400</option>
<option value="1500">1500</option>
<option value="1600">1600</option>
<option value="1700">1700</option>
<option value="1800">1800</option>
<option value="1900">1900</option>
<option value="2000">2000</option>
</select>
<button onclick="calculate()">Calculate</button>
<input type="text" id="output" />
</body>
答案 0 :(得分:1)
您在代码中犯了很多错误和错误。
首先,这是一个有效的代码段,然后我提供一些注释,可能会对您有所帮助:
function calculate() {
var numsessions = parseInt(document.getElementById("numsessions").value), timeofday = document.getElementById("timeofday").value, dayvar = document.getElementById("day").value, price = "";
if (timeofday == "0700" || timeofday == "0800") {
price = 25;
}
if (timeofday == "0900" || timeofday == "1000" || timeofday == "1100") {
//price = "Normal Rates";
price = 25;
}
if (timeofday == "1200" || timeofday == "1300") {
if (day = "Saturday") {
price = 20;
}
else {
price = 30;
}
}
if (timeofday == "1400" || timeofday == "1500") {
price = 20;
}
if (timeofday == "1600" || timeofday == "1700") {
if (day = "Saturday") {
price = 20;
}
else {
//price = "Normal Rates";
price = 25;
}
}
if (timeofday == "1800") {
//price = "Normal Rates";
price = 25;
}
if (timeofday == "1900") {
price = 30;
}
if (timeofday == "2000") {
price = 20;
}
//Output
var total = price * numsessions, output = "£" + total + ".00";
document.getElementById("output").value = output;
}
<input type="number" id="numsessions" min="1" max="5" value="1" />
<select id="day">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
</select>
<select id="timeofday">
<option value="0700">0700</option>
<option value="0800">0800</option>
<option value="0900">0900</option>
<option value="1000">1000</option>
<option value="1100">1100</option>
<option value="1200">1200</option>
<option value="1300">1300</option>
<option value="1400">1400</option>
<option value="1500">1500</option>
<option value="1600">1600</option>
<option value="1700">1700</option>
<option value="1800">1800</option>
<option value="1900">1900</option>
<option value="2000">2000</option>
</select>
<button onclick="calculate()">Calculate</button>
<input type="text" id="output" />
一些注释和解释:
price
可以采用string
作为值,导致输出中的NaN
。在上面的代码段中,我将所有这些行price = "Normal Rate";
更改为price = 25;
。numbers
分配为strings
(price = "35"
),此方法有效,并且JavaScript
会在计算时将其转换为数字,但是,您想要数字为何将它们分配为琴弦? =
进行比较,那确实是错误的。您必须使用==
或===
进行比较。=
用于分配。==
用于值比较。===
用于值和类型比较。HTML
仅用于页面结构/标记。您应该使用JavaScript
来完成任务。希望我进一步推动了你。