function myfunction(){
  if (document.getElementById('name1').value!="liu"){
   alert("用户名错误"); return false;}
  else if(document.getElementById('password1').value!="123456") {
 alert('密码错误');return false;}
  else{alert('登录成功');}
}

解决方案 »

  1.   

    document.getElementById('name1')            应该是 document.getElementById('name1') .value()   这才是获取值 
      

  2.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="textml; charset=utf-8" /> 
    <title>swcss</title>   
    <style type="text/css">   
    .linear{   
        width:30%;   
        height:160px;   
        background:-webkit-gradient(linear, 0% 0%, 0% 100%,from(#FFF),to(#CCC));/*谷歌*/ 
        opacity:0.8;   

    body{
        background:url(8.jpg); 
    background-repeat:no-repeat; 
    background-size:cover;
    }
    </style> 
    </head>  
     <body>  
       <form >
       <div class="linear" align="center" style="text-align:center; margin-left:34%;margin-top:20%;">
     <table align="center" valign="middle" style="text-align:center; margin-top:15%; padding-top:10%;">
        <tr>
        <td>用户名</td>
        <td><input type="text" id="name1" name="name"></td>
        </tr> 
        <tr>
        <td>密码</td>
        <td><input type="password" id="password1" name="password"></td>
        </tr>
        <tr>
        <td></td>
        <td><input type="submit" value="Log in" onclick="myfunction()" style=" background-color:#09F;font-size:22px;">
        </td>
        </tr>
       </table>
       </div>
       </form> 
       
    <script type="text/javascript">
    function myfunction(){
      if (document.getElementById("name1").value!="liu"){
       alert("用户名错误"); return false;}
      if(document.getElementById("password1").value!="123456"){
     alert('密码错误');return false;}
      else{alert('登录成功');}
    }
    </script>  
    </body>  
    <html>  
      

  3.   

       你先alert一下 (document.getElementById("name1").value   看看 值什么   应该没取到
      

  4.   

    没问题啊
    <input type="submit" value="Log in" onclick="return myfunction()" style=" background-color:#09F;font-size:22px;">
      

  5.   

    提交按钮改成这样
    ><input type="submit" value="Log in" onclick="return myfunction()" style=" background-color:#09F;font-size:22px;">
    函数改成这样
    function myfunction(){
      if (document.getElementById("name1").value!="liu"){
       alert("用户名错误"); return false;}
      if(document.getElementById("password1").value!="123456"){
     alert('密码错误');return false;}
      else{alert('登录成功');}
      return true;
    }
      

  6.   

    大神,成功了,不过我可不可以问一下,
    onclick="return myfunction()" 
     return true;
    是什么,加不加它有很大区别吗