My code below is a simple form which takes an integer, and a yes/no radio button pair. If ‘yes’ is selected (the left option) then the value you entered * 0.13 should be displayed – if ‘no’ is selected (or neither are selected) then the value 0 should be displayed. It is not working as expected – how can I fix this problem?
JavaScript
x
42
42
1
<html lang="ru">
2
3
<head>
4
<meta charset="utf-8">
5
<title>Калькулятор</title>
6
</head>
7
8
9
<body>
10
11
<FORM NAME="Form1">
12
<p><b>Какая у вас ЗП?</b></p>
13
<INPUT TYPE="number" id="salary" class="value" placeholder="Enter an integer" />
14
15
<p><b>Ваш подоходный налог 13%?</b></p>
16
<INPUT TYPE="radio" name="Nalog" id="da" value="Yes">Да
17
18
<INPUT TYPE="radio" name="Nalog" id="net" value="No">Нет
19
20
<br>Посчитаем сумму налога?
21
<INPUT TYPE ="button" value="Посчитать" onClick="solve();">
22
23
<p id="result"></p>
24
</form>
25
26
<script>
27
var output = document.getElementById("result");
28
29
function solve() {
30
var input = document.getElementById("salary").value;
31
output.innerHTML = "";
32
if (document.Form1.nalog[0].checked) {
33
output.innerHTML = input * 0.13;
34
} else {
35
output.innerHTML = 0;
36
}
37
</script>
38
39
</body>
40
41
</html>
42
Advertisement
Answer
The issues I found are as follows:
- The script hasn’t been evaluated yet so
solve
is not defined. I’ve used an ID on that button and added an event listener instead. - You should be accessing
Form1.Nalog
notForm1.nalog
(note the capitalisation of the name attribute matters).
Here’s a working demo with those changes applied:
JavaScript
1
45
45
1
<html lang="ru">
2
3
<head>
4
<meta charset="utf-8">
5
<title>Калькулятор</title>
6
</head>
7
8
9
<body>
10
11
<form NAME="Form1">
12
<p><b>Какая у вас ЗП?</b></p>
13
<INPUT TYPE="number" id="salary" class="value" placeholder="Enter an integer" />
14
15
<p><b>Ваш подоходный налог 13%?</b></p>
16
<INPUT TYPE="radio" name="Nalog" id="da" value="Yes">Да
17
18
<INPUT TYPE="radio" name="Nalog" id="net" value="No">Нет
19
20
<br>Посчитаем сумму налога?
21
<INPUT TYPE="button" value="Посчитать" id="submit">
22
23
<p id="result"></p>
24
</form>
25
26
<script>
27
var output = document.getElementById("result");
28
var btn = document.getElementById("submit");
29
30
btn.addEventListener("click", solve);
31
32
function solve() {
33
var input = document.getElementById("salary").value;
34
output.innerHTML = "";
35
if (document.Form1.Nalog[0].checked) {
36
output.innerHTML = input * 0.13;
37
} else {
38
output.innerHTML = 0;
39
}
40
}
41
</script>
42
43
</body>
44
45
</html>