PDA

View Full Version : calculate expression with only statement,help me


crazyman
02-19-2008, 03:54 AM
Somebody please tell me why my code don't work, when i reference agrument by function ,it dislay my text box not a object??where is my misstake?thank you very much.

<!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=iso-8859-1" />
<title>Untitled Document</title>
<script>
function check(num)
{
if(num=="")
{
alert(" please enter a number")
}

if(isNaN(num) )
{
alert("please enter a number value")
}

}

function calMath(form)
{
with(form)
{
document.myform.rt.value= eval(box1.value + ra1.value + box2.value)
}

}

</script>
</head>

<body>
<form name="myform">
<table>
<tr>
<td>Number 1:</td><td> <input name="box1" onblur="check(this.value)"/></td>
<tr ><td colspan="2" align="center"><input type="radio" name="ra1" value="+" />+
<input type="radio" name="ra1" value="-" />-
<input type="radio" name="ra1" value="*" />*
<input type="radio" name="ra1" value="/" />/</td>
<tr>
<td>Number 2:</td><td> <input name="box2" onblur="check(this.value)" /></td>
<tr><td><input type="button" value="Result" onclick="calMath(this.form)" /></td><td> <input name="rt" type="text" /></td>

</table>
</form>
</body>
</html>

chikna
02-19-2008, 06:46 AM
<script>
function check(num)
{
if(num.value=="")
{
alert(" please enter a number")
}

if(isNaN(num.value) )
{
alert("please enter a number value")
}

}

function calMath(thisform) {
var a =0,option;
for (i=0; i < thisform.ra1.length; i++) {
if (thisform.ra1[i].checked) {
option = thisform.ra1[i].value;
}
}
if (option == "") {
alert("You must select a radio button");
return false;
}

switch (option) {
case '+':
a = Number(thisform.box1.value) + Number(thisform.box2.value);
break;
case '-':
a = Number(thisform.box1.value) - Number(thisform.box2.value);
break;
case '*':
a = Number(thisform.box1.value) * Number(thisform.box2.value);
break;
case '/':
a = Number(thisform.box1.value) / Number(thisform.box2.value);
break;
}
thisform.rt.value = a;
}

</script>
</head>

<body>
<form name="myform">
<table>
<tr>
<td>Number 1:</td><td> <input name="box1" onblur="check(this)"/></td>
<tr ><td colspan="2" align="center"><input type="radio" name="ra1" value="+" />+
<input type="radio" name="ra1" value="-" />-
<input type="radio" name="ra1" value="*" />*
<input type="radio" name="ra1" value="/" />/</td>
<tr>
<td>Number 2:</td><td> <input name="box2" onblur="check(this)" /></td>
<tr><td><input type="button" value="Result" onclick="calMath(myform); return false;" /></td><td> <input name="rt" type="text" /></td>

</table>
</form>
</body>
</html>

crazyman
02-19-2008, 08:48 AM
Thank you if that so i can do but my teacher want me must calculate with only satement not by some statement.He want me calculate by "eval" statement.

Kor
02-19-2008, 08:56 AM
Thank you if that so i can do but my teacher want me must calculate with only satement not by some statement.He want me calculate by "eval" statement.
Tell your teacher he's a moron. eval() method has to be avoided whenever is possible. It eats processors resources and, in some cases, it might be even dangerous. The eval() is used mainly in manipulating the JSON objects, and even there with care...

And, anyway, eval() is not a statement, it is a method