以下程序javascript部分第二句执行不过去,为什么???????
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>实验3.1</title>
<script type="text/javascript" src="test.js">
</script>
</head>
<body>
<form id="fruit">
<label><input type="radio" name="apple" value="2"/>
苹果(2元)</label>
<label><input type="radio" name="orange" value="3"/>
桔子(3元)</label>
<label><input type="radio" name="banana" value="4"/>
香蕉(4元)</label>
<label><input type="button" value="total cost" onclick="computeCost()"/>
</label>
<label>总计:<input type="text" name="cost"/></label>
</form>
</body>
</html>

javascript部分
function computeCost(){
var dom=document.getElementById("fruit");
dom.cost.value=dom.apple.value+dom.orange.value;
}javascriptinput

解决方案 »

  1.   

    你的加操作时字符串相加  而不是数字 自己用parseInt转一下 
    不明白你的意思
      

  2.   

    执行没有任何错误,只是结果不是两个数相加的结果(2+3=5),而是两个字符串相加的结果(“2”+“3”=“23”),相加之前应先进行类型转换dom.cost.value = parseInt(dom.apple.value)+parseInt(dom.orange.value),你用的是什么浏览器?还是因为你的js文件没找到(<script type="text/javascript" src="test.js">你的js文件应该和你的页面放在同级目录下)?
      

  3.   

    function computeCost(){
    var dom=document.getElementById("fruit");
    dom.cost.value=(+dom.apple.value)+(+dom.orange.value);
    }