注册 登录
编程论坛 JavaScript论坛

请各位高手帮我看看

kevin6639 发布于 2011-09-03 20:37, 690 次点击
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript">
function CheckFields()
{
    var fieldvalue = document.getElementById("name").value;
    if (fieldvalue == "")
    {
        alert("欄位不可以是空白!");
        document.getElementById("name").focus();
        return false;
    }            
    return true;
}
</script>
</head>

<body>
<form action="index.php" method="post">
帳號:<input name="name" type="text" />
<input name="" type="submit" value="取消送出" onclick="return CheckFields();"/>
</form>
</body>
</html>

请问是我写错了什么吗?为什么我送出空白的时候不会弹出“欄位不可以是空白”这句话的呢?
5 回复
#2
gulimeksoft2011-09-05 15:49
帳號:<input name="name" type="text" />
没有ID ,加上 id="name"就可以了
#3
刘杰明2011-09-06 20:41
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript">
function CheckFields()
{
    var fieldvalue = document.userform.name.value;
    if (fieldvalue == "")
    {
        alert("欄位不可以是空白!");
        document.getElementById("name").focus();
        return false;
    }            
    return true;
}
</script>
</head>

<body>
<form name ="userform" action="index.php" method="post">
帳號:<input name="name" type="text" />
<input name="" type="button" value="取消送出" onclick="CheckFields();"/>
</form>
</body>
</html>
这样就可以了,首先需要告诉你的是,在你的form中需要有一个名字,还有就是在定义变量的时候可以这样定义    var fieldvalue = document.userform.name.value;
(document.form的名字、控件的名字.value)
方法的名字要和你onclick中一样才可以被调用的啊,呵呵,我也是前两天才学的javascript
#4
刘杰明2011-09-06 20:53
哦,对了,你最后那个按钮地方,需要用type="button"(按钮)而不是"submit"(提交)
#5
wangxianping2011-09-09 13:39
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript">
function CheckFields()
{
    var fieldvalue = document.getElementById("name").value;
    if (fieldvalue == "")
    {
        alert("欄位不可以是空白!");
        document.getElementById("name").focus();
        return false;
    }            
    return true;
}
</script>
</head>

<body>
<form action="index.php" method="post" onSubmit="return CheckFields();"/ >
帳號:<input id="name" type="text" />
<input name="" type="submit" value="取消送出" />
</form>
</body>
</html>

1