javascript内置函数调用问题: 代码如下,调用函数不成功. 请教……
<htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type"content...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>计算</title>
<script language="javascript" type="text/javascript">
function coms(op){
var num1,num2;
num1=parseFloat(document.myform.txtNum1.value);
num2=parseFloat(document.myform.txtNum2.value);
if(op=="+")
document.myform.txts.value=num1+num2;
if(op=="-")
document.myform.txts.value=num1-num2;
if(op=="*")
document.myform.txts.value=num1*num2;
if(op=="/" && num2!=0)
document.myform.txts.value=num1/num2;
}
</script>
</head>
<body>
<form action="" name="myform" id="myform">
<table id="op">
<tr>
<td><input name="txtNum1" type="text" id="txtNum1"/></td>
</tr>
<tr>
<td>
<select>
<option value="+"> + </option>
<option value="-"> - </option>
<option value="*"> × </option>
<option value="/"> ÷ </option>
</select>
</td>
</tr>
<tr>
<td><input name="txtNum2" type="text" id="txtNum2"/>
</td>
</tr>
<tr>
<td><input name="but" type="button" onclick="coms()" value="计算"/></td>
</tr>
<tr>
<td><input name="txts" type="text" id="txts"/></td>
</tr>
</table>
</form>
</body>
</html> 展开
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>计算</title>
<script language="javascript" type="text/javascript">
function coms(op){
var num1,num2;
num1=parseFloat(document.myform.txtNum1.value);
num2=parseFloat(document.myform.txtNum2.value);
if(op=="+")
document.myform.txts.value=num1+num2;
if(op=="-")
document.myform.txts.value=num1-num2;
if(op=="*")
document.myform.txts.value=num1*num2;
if(op=="/" && num2!=0)
document.myform.txts.value=num1/num2;
}
</script>
</head>
<body>
<form action="" name="myform" id="myform">
<table id="op">
<tr>
<td><input name="txtNum1" type="text" id="txtNum1"/></td>
</tr>
<tr>
<td>
<select>
<option value="+"> + </option>
<option value="-"> - </option>
<option value="*"> × </option>
<option value="/"> ÷ </option>
</select>
</td>
</tr>
<tr>
<td><input name="txtNum2" type="text" id="txtNum2"/>
</td>
</tr>
<tr>
<td><input name="but" type="button" onclick="coms()" value="计算"/></td>
</tr>
<tr>
<td><input name="txts" type="text" id="txts"/></td>
</tr>
</table>
</form>
</body>
</html> 展开
1个回答
展开全部
不是因为内置函数的问题,而是你调用coms函数的时候的参数没传过去,我改了的代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>计算</title>
<script language="javascript" type="text/javascript">
function coms(op){
var num1,num2;
num1=parseFloat(document.myform.txtNum1.value);
num2=parseFloat(document.myform.txtNum2.value);
if(op=="+")
document.myform.txts.value=num1+num2;
if(op=="-")
document.myform.txts.value=num1-num2;
if(op=="*")
document.myform.txts.value=num1*num2;
if(op=="/" && num2!=0)
document.myform.txts.value=num1/num2;
}
</script>
</head>
<body>
<form action="" name="myform" id="myform">
<table id="op">
<tr>
<td><input name="txtNum1" type="text" id="txtNum1"/></td>
</tr>
<tr>
<td>
<select id="select">
<option value="+"> + </option>
<option value="-"> - </option>
<option value="*"> × </option>
<option value="/"> ÷ </option>
</select>
</td>
</tr>
<tr>
<td><input name="txtNum2" type="text" id="txtNum2"/>
</td>
</tr>
<tr>
<td><input name="but" type="button" onClick="coms(document.getElementById('select').value)" value="计算"/></td>
</tr>
<tr>
<td><input name="txts" type="text" id="txts"/></td>
</tr>
</table>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>计算</title>
<script language="javascript" type="text/javascript">
function coms(op){
var num1,num2;
num1=parseFloat(document.myform.txtNum1.value);
num2=parseFloat(document.myform.txtNum2.value);
if(op=="+")
document.myform.txts.value=num1+num2;
if(op=="-")
document.myform.txts.value=num1-num2;
if(op=="*")
document.myform.txts.value=num1*num2;
if(op=="/" && num2!=0)
document.myform.txts.value=num1/num2;
}
</script>
</head>
<body>
<form action="" name="myform" id="myform">
<table id="op">
<tr>
<td><input name="txtNum1" type="text" id="txtNum1"/></td>
</tr>
<tr>
<td>
<select id="select">
<option value="+"> + </option>
<option value="-"> - </option>
<option value="*"> × </option>
<option value="/"> ÷ </option>
</select>
</td>
</tr>
<tr>
<td><input name="txtNum2" type="text" id="txtNum2"/>
</td>
</tr>
<tr>
<td><input name="but" type="button" onClick="coms(document.getElementById('select').value)" value="计算"/></td>
</tr>
<tr>
<td><input name="txts" type="text" id="txts"/></td>
</tr>
</table>
</form>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询