<script language="javascript" type="text/javascript">
function mysubmit(){
………………
if(username==""){alert("用户名不能为空!");return false;}
}
<form name="theform" method ="post" action="1.asp" onsubmit ="return mysubmit();">
<input type=text name="username">
<input type=submit value="提交">
IE浏览器没有问题,但是Firefox浏览器里却不行:文本框里没有填写数据,点击提交也能通过。

解决方案 »

  1.   

    ff中不能使用元素名称直接访问元素对象的,要使用document.getElementById("id")
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>    <script language="javascript" type="text/javascript">
            function mysubmit()
            {
                var username = document.getElementById("username").value;
                if (username == "")
                {
                    alert("用户名不能为空!");
                    return false;
                }
            }
        </script></head>
    <body>
        <form name="theform" method="post" action="1.asp" onsubmit="return mysubmit();">
        <input type="text" id="username" name="username">
        <input type="submit" value="提交">
        </form>
    </body>
    </html>
      

  2.   

    楼主:红色部分你的  username 找不到对应的 input<form name="theform" method ="post" action="1.asp" onsubmit ="return mysubmit();">
        <input type=text name="username" id="btn"
        <input type=submit value="提交">
    </form><script language="javascript" type="text/javascript"> 
    function mysubmit(){ 
        var btn = document.getElementById('btn');
        if(btn.value == ""){
    alert("用户名不能为空!");
    return false;
    }
    }
    </script>