本人小菜一个.请教一个简单的问题
<html>
<head>
<title> Page 2</title>
<script language="javascript">
<!--
function checkbox(){
if(document.form.submit.value==""){
alert("please input username");
document.form.number.focus();
}
}
-->
</script>
</head>
<body>
<form>
<table><tr>
<td> user name </td>
<td> <input type="text" name="username" size="15"</td>
<td><input type="submit" name="number" value="submit" onClick="checkbox()">|<input type="submit" value="reset"></td>
<td> code </td>
<td> <input type="text" name="code" size="15"</td>
</tr></table></form>
</body>
</html>
这段代码里为什么在 if(document.form.submit.value==""){
alert("please input username");
document.form.number.focus();
}
}
document.form.submit.value=="" 老是为假?就是我在页面中TEXT框里即使没东西submit的值也不为空啊?
<html>
<head>
<title> Page 2</title>
<script language="javascript">
<!--
function checkbox(){
if(document.form.submit.value==""){
alert("please input username");
document.form.number.focus();
}
}
-->
</script>
</head>
<body>
<form>
<table><tr>
<td> user name </td>
<td> <input type="text" name="username" size="15"</td>
<td><input type="submit" name="number" value="submit" onClick="checkbox()">|<input type="submit" value="reset"></td>
<td> code </td>
<td> <input type="text" name="code" size="15"</td>
</tr></table></form>
</body>
</html>
这段代码里为什么在 if(document.form.submit.value==""){
alert("please input username");
document.form.number.focus();
}
}
document.form.submit.value=="" 老是为假?就是我在页面中TEXT框里即使没东西submit的值也不为空啊?
没输入怎么不为空?
<html>
<head>
<title> Page 2 </title>
<script language="javascript">
<!--
function checkbox(){
if(document.getElementById("username").value==""){
alert("please input username");
document.form.number.focus(); }
}
-->
</script>
</head>
<body>
<form>
<table> <tr>
<td> user name </td>
<td> <input type="text" id="username" name="username" size="15"> </td>
<td> <input type="submit" name="number" value="submit" onClick="checkbox()">| <input type="submit" value="reset"> </td>
<td> code </td>
<td> <input type="text" name="code" size="15"></td>
</tr>
</table> </form>
</body>
</html>
非常感谢,不过还有个小小的问题,我的document.form.submit.value==""这句话,到底错在哪里呢?
<html>
<head>
<title> Page 2 </title>
<script language="javascript">
<!--
function checkbox(){
if(document.getElementsByName("number")[0].value=""){
alert("please input username");
document.getElementsByName("number")[0].focus();
}
}
-->
</script>
</head>
<body>
<form>
<table> <tr>
<td> user name </td>
<td> <input type="text" name="username" size="15" </td>
<td> <input type="submit" name="number" value="submit" onClick="checkbox()">| <input type="submit" value="reset"> </td>
<td> code </td>
<td> <input type="text" name="code" size="15" </td>
</tr>
</table> </form>
</body>
</html>
这个..有兼容性问题,最好用getElementsByName,getElementsByTagName,getElementById的方法来获取dom元素
直接用元素的name属性做为引用的话,有兼容性问题,并不是像书所说的那样,可以直接引用,比如document.body.***还有再说个问题
你的只要点submit键,在没有外界的干扰下,系统一般就会自动地去触发submit事件,去刷新页面,所以原本的一些效果你看不到了,所以你要试一些东西最好用button来,不要用submit,即使它可能不是你的本意.
alert("please input username");
}