日期:2014-05-17  浏览次数:20668 次

初学javascipt 请问怎么动态传入控件名?
请看 checkNum(form1.num1) 函数



JScript code


<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<script language="javascript">
function checkNum(this)
{
  if(this.value=="")
  {
   window.alert("空");
   }
   
}
</script>
</head>
<body>

<form name="form1" action="myresult.jsp" >
<input type="text" name="numl"><br>
<select name="flag">
<option value=+>+</option>
<option value=->-</option>
<option value=*>*</option>
<option value=/>/</option>
</select>
<br>
<input type="text" name="num2"><br>
<input type="submit" onclick="checkNum(form1.num1)" value="计算">
</form>
</body>
</html>



------解决方案--------------------
HTML code

<html>
<head>
<script language="javascript">
function checkNum(o)//不能用this关键字
{
  if(o.value=="")
  {
   window.alert("空");
   }
   alert(o.value);
}
</script>
</head>
<body>

<form name="form1" action="myresult.jsp" >
<input type="text" name="num1"><br>num1拼写错误
<select name="flag">
<option value=+>+</option>
<option value=->-</option>
<option value=*>*</option>
<option value=/>/</option>
</select>
<br>
<input type="text" name="num2"><br>
<input type="submit" onclick="checkNum(form1.num1)" value="计算">
</form>
</body>
</html>