Javascript单选按钮 [英] Javascript sum of radio button
本文介绍了Javascript单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以知道我的错误在哪里,因为它看起来不起作用
May i know where is my error because its seen like not working
<?xml version = "1.0" encoding = "utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Pratical Lab 5.5-->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head><title> Practical Lab 5.5</title>
<script type = "text/javascript" src = "sum.js"></script></head>
<form name = "myForm" action = "" >
Motherboard (RM59 each) <input type="radio" id="value1" name="value1" onclick ="59"/>
<br />
Harddisk (RM49 each) <input type="radio" id="value2" name="value2" onclick ="49"/>
<br />
Keyboard (RM39 each) <input type="radio" id="value3" name="value3" onclick ="39"/>
<br />
<input type="button" name="Sumbit" value="Submit" onclick="javascript:addNumbers()"/>
<br />
Total including 5% sales tax= <input type="text" id="answer" name="answer" value=""/>
</form>
</body>
</html>
这是我的java脚本
and this is my javascript
function addNumbers()
{
var val1 = parseInt(document.getElementById("value1").59);
var val2 = parseInt(document.getElementById("value2").49);
var val3 = parseInt(document.getElementById("value3").39);
var ansD = document.getElementById("answer");
ansD.value = val1 + val2 + val3;
}
推荐答案
整个想法是错误的:单选按钮不带数值。你需要解释为什么你需要的东西就像你做的那样,在应用方面。
属性onclick
是为点击事件处理程序而设计的,而不是数字数据。 JavaScript代码已包含数字59,49,39,因此即使它起作用也不会用于任何目的。我认为进一步的分析没有任何意义。
-SA
The whole idea is wrong: the radio buttons do not carry numeric values. You need to explain why would you need something like what you do, in terms of application.
The attributeonclick
is designed for a click event handler, not numeric data. The JavaScript code already contains numbers 59, 49, 39, so it would not serve any purpose even if it worked. I don't think further analysis makes any sense.
—SA
它抛出了错误'未捕获的SyntaxError:意外的数字sum.js:3'。因为document.getElementById(value1)应该使用Element属性之一。相反,你的代码有不正确的数字,如59,49和39.
你想在那里实现什么?
It throws the error 'Uncaught SyntaxError: Unexpected number sum.js:3'. Because document.getElementById("value1") should use one of the Element property. Instead your code has the improper numbers like 59, 49 and 39.
What do you want to achieve there?
I纠正了错误并做了一些像这样的事情,但点击放气处理程序不适合我,我没有选择单选按钮并单击提交,它显示输出给我
I had correct the error and made somethings like this, but the click vent handler not work for me, i haven choose the radio button and click submit, it show the output to me
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head><title> Practical Lab 5.5</title>
<script type = "text/javascript" src = "sum.js"></script></head>
<body>
<form name = "myForm" action = "" >
Motherboard (RM59 each) <input type="radio" id="value1" name="value1" onclick ="addNumbers(val1)" value="59" />
<br />
Harddisk (RM49 each) <input type="radio" id="value2" name="value2" onclick ="addNumbers(val2)" value="49" />
<br />
Keyboard (RM39 each) <input type="radio" id="value3" name="value3" onclick ="addNumbers(val3)" value="39" />
<br />
<input type="button" name="Sumbit" value="Submit" onclick="javascript:addNumbers()"/>
<br />
Total cost including 5% tax = <input type="text" id="answer" name="answer" value=""/>
</form>
</body>
function addNumbers()
{
var val1 = parseInt(document.getElementById("value1").value);
var val2 = parseInt(document.getElementById("value2").value);
var val3 = parseInt(document.getElementById("value3").value);
var ansD = document.getElementById("answer");
ansD.value = val1 + val2 + val3 + 7.35;
}
这篇关于Javascript单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文