日期:2014-05-18  浏览次数:20704 次

radio提交问题
<html>
<body>
<input   type= "text "   name= "num1 "   > <br>

<input   type= "text "   name= "num2 "> <br>

<input   type= "radio "   name= "radio1 "> +
&nbsp

<input   type= "radio "   name= "radio2 "> -
&nbsp

<input   type= "radio "   name= "radio3 "> *
&nbsp

<input   type= "radio "   name= "radio4 "> / <br>

<input   type= "text "   name= "console "> <br>
</body>
</html>

从上面两个文本框中输入数字,如何在点击一个radio的时候,把相应的计算结果显示到最后一个文本框中,同时alert出结果,这个代码该怎么写啊?

------解决方案--------------------
无校验,不建议在js中进行除法运算、浮点值的* /运算。
<html>
<head>
<script language= "JavaScript " type= "text/JavaScript ">
function doMath(flag){

var num1,num2,sum;
num1 = document.form1.num1.value;
num2 = document.form1.num2.value;

if(flag== '+ '){
sum = parseInt(num1,10)+parseInt(num2,10);
document.form1.console.value = sum;
alert( "你要的答案: "+sum);
}else if(flag== '- '){
sum = parseInt(num1,10)-parseInt(num2,10);
document.form1.console.value = sum;
alert( "你要的答案: "+sum);
}else if(flag== '* '){
sum = parseInt(num1,10)*parseInt(num2,10);
document.form1.console.value = sum;
alert( "你要的答案: "+sum);
}else if(flag== '/ '){
sum = parseInt(num1,10)/parseInt(num2,10);
document.form1.console.value = sum;
alert( "你要的答案: "+sum);
}
}
</script>
</head>
<body>
<form name= 'form1 '>
<input type= "text " name= "num1 " > <br>

<input type= "text " name= "num2 "> <br>

<input type= "radio " name= "radio " onclick=javascript:doMath( '+ ')> +
&nbsp

<input type= "radio " name= "radio " onclick=javascript:doMath( '- ')> -
&nbsp

<input type= "radio " name= "radio " onclick=javascript:doMath( '* ')> *
&nbsp

<input type= "radio " name= "radio " onclick=javascript:doMath( '/ ')> / <br>

<input type= "text " name= "console " > <br>
</form>
</body>
</html>