可以在HTML页面上获取JS脚本输出数据
我下面的代码是一个简单的表单,它需要一个整数,是/否广播按钮对。如果选择“是”(左选项),则应显示您输入的值 * 0.13 - 如果选择'否'(或不选择),则应显示值0。它无法正常工作 - 我该如何解决此问题?
<html lang="ru">
<head>
<meta charset="utf-8">
<title>Калькулятор</title>
</head>
<body>
<FORM NAME="Form1">
<p><b>Какая у вас ЗП?</b></p>
<INPUT TYPE="number" id="salary" class="value" placeholder="Enter an integer" />
<p><b>Ваш подоходный налог 13%?</b></p>
<INPUT TYPE="radio" name="Nalog" id="da" value="Yes">Да
<INPUT TYPE="radio" name="Nalog" id="net" value="No">Нет
<br>Посчитаем сумму налога?
<INPUT TYPE ="button" value="Посчитать" onClick="solve();">
<p id="result"></p>
</form>
<script>
var output = document.getElementById("result");
function solve() {
var input = document.getElementById("salary").value;
output.innerHTML = "";
if (document.Form1.nalog[0].checked) {
output.innerHTML = input * 0.13;
} else {
output.innerHTML = 0;
}
</script>
</body>
</html>
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?
<html lang="ru">
<head>
<meta charset="utf-8">
<title>Калькулятор</title>
</head>
<body>
<FORM NAME="Form1">
<p><b>Какая у вас ЗП?</b></p>
<INPUT TYPE="number" id="salary" class="value" placeholder="Enter an integer" />
<p><b>Ваш подоходный налог 13%?</b></p>
<INPUT TYPE="radio" name="Nalog" id="da" value="Yes">Да
<INPUT TYPE="radio" name="Nalog" id="net" value="No">Нет
<br>Посчитаем сумму налога?
<INPUT TYPE ="button" value="Посчитать" onClick="solve();">
<p id="result"></p>
</form>
<script>
var output = document.getElementById("result");
function solve() {
var input = document.getElementById("salary").value;
output.innerHTML = "";
if (document.Form1.nalog[0].checked) {
output.innerHTML = input * 0.13;
} else {
output.innerHTML = 0;
}
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现的问题如下:
solve
尚未定义。我在该按钮上使用了ID,然后添加了事件侦听器。form1.nalog
不是form1.nalog
(请注意名称属性事项的大写)。这是适用这些更改的工作演示:
The issues I found are as follows:
solve
is not defined. I've used an ID on that button and added an event listener instead.Form1.Nalog
notForm1.nalog
(note the capitalisation of the name attribute matters).Here's a working demo with those changes applied: