<html>
<head>
</head>
<body><a>
<script type="text/javascript">
window.onload = function(){
showval();
$('username').focus();

$('username').onkeydown = function(){
if(event.keyCode == 13){
$('password').select();
}
}
$('password').onkeydown = function(){
if(event.keyCode == 13){
chklg();
window.open('http://www.sohu.com','found','width=300,height=200')
}
} function test(){
chklog();
window.open('http://192.168.0.253/search/search.html','found','width=300,height=200')
}

function chklg(){
if($('username').value != "sa"){
alert('用户名输入错误');
$('username').select();
return false;
}
if($('username').value == ''){
alert('请输入用户名!');
$('username').focus();
return false;
}
if($('password').value != "sa"){
alert('密码输入错误');
$('password').select();
return false;
}
if($('password').value == ''){
alert('请输入密码!');
$('password').focus();
return false;
}
}</script>
<div style=" border:1px #ddd solid; overflow:hidden;text-align:center; float:center; margin:100px;">
<div style="textalign=center; margin-top:10px; margin-left:auto">
<div><p>用户名:<input type=text; style="width:160px"; id="username"; name="username"></input></p></div><div id=usr></div>
</div>
<div style="position:relative; margin-top:10px; margin-left:auto;">
<div><p>密码:   <input type="password"; style="width:160px"; id=password; name=password></input></p></div>
<div id=pwd></div>
</div>
<div style="margin-top:10px;margin-left:50px;"><input style="width:70px" type="submit"; value="登录" onclick='test()'></input></div>
</div>
</a></body></html>

解决方案 »

  1.   

    这里的$('username') 这样的形式是什么意思?是php的还是js的代码? 
      

  2.   

    你没用定义 $ 函数,当然不行
    function $(e) {
      return document.getElementById(e);
    }
      

  3.   

    按照3楼的代码写成如下,也不行啊
    <html>
    <head>
    </head>
    <body><a>
    <script type="text/javascript">
    window.onload = function(){
    function $(id){
    return document.getElementById(id);
    }
    $('username').focus();

    $('username').onkeydown = function(){
    if(event.keyCode == 13){
    $('password').select();
    }
    }
    $('password').onkeydown = function(){
    if(event.keyCode == 13){
    chklg();
    window.open('http://www.sohu.com','found','width=300,height=200')
    }
    } function test(){
    chklog();
    window.open('http://192.168.0.253/search/search.html','found','width=300,height=200')
    }

    function chklg(){
    if($('username').value != "sa"){
    alert('用户名输入错误');
    $('username').select();
    return false;
    }
    if($('username').value == ''){
    alert('请输入用户名!');
    $('username').focus();
    return false;
    }
    if($('password').value != "sa"){
    alert('密码输入错误');
    $('password').select();
    return false;
    }
    if($('password').value == ''){
    alert('请输入密码!');
    $('password').focus();
    return false;
    }
    }</script>
    <div style=" border:1px #ddd solid; overflow:hidden;text-align:center; float:center; margin:100px;">
    <div style="textalign=center; margin-top:10px; margin-left:auto">
    <div><p>用户名:<input type=text; style="width:160px"; id="username"; name="username"></input></p></div><div id=usr></div>
    </div>
    <div style="position:relative; margin-top:10px; margin-left:auto;">
    <div><p>密码:   <input type="password"; style="width:160px"; id=password; name=password></input></p></div>
    <div id=pwd></div>
    </div>
    <div style="margin-top:10px;margin-left:50px;"><input style="width:70px" type="submit"; value="登录" onclick="test()"></input></div>
    </div>
    </a></body></html>
      

  4.   

    还是不行啊, 是不是代码哪里出了问题啊?加了function $(e) {
      return document.getElementById(e);
    } 这段函数也不行啊
      

  5.   

    要定义在外面
    function $(id){
    return document.getElementById(id);
    }
    window.onload = function(){
    $('username').focus();

      

  6.   

    奇怪了,写到外面了还是不行,能帮我看一下么
    <html>
    <head>
    </head>
    <body><a>
    <script type="text/javascript">
    function $(id){
    return document.getElementById(id);
    }
    window.onload = function(){ $('username').focus();

    $('username').onkeydown = function(){
    if(event.keyCode == 13){
    $('password').select();
    }
    }
    $('password').onkeydown = function(){
    if(event.keyCode == 13){
    chklg();
    window.open('http://www.sohu.com','found','width=300,height=200')
    }
    } function test(){
    chklog();
    window.open('http://192.168.0.253/search/search.html','found','width=300,height=200')
    }

    function chklg(){
    if($('username').value != "sa"){
    alert('用户名输入错误');
    $('username').select();
    return false;
    }
    if($('username').value == ''){
    alert('请输入用户名!');
    $('username').focus();
    return false;
    }
    if($('password').value != "sa"){
    alert('密码输入错误');
    $('password').select();
    return false;
    }
    if($('password').value == ''){
    alert('请输入密码!');
    $('password').focus();
    return false;
    }
    }</script>
    <div style=" border:1px #ddd solid; overflow:hidden;text-align:center; float:center; margin:100px;">
    <div style="textalign=center; margin-top:10px; margin-left:auto">
    <div><p>用户名:<input type=text; style="width:160px"; id="username"; name="username"></input></p></div><div id=usr></div>
    </div>
    <div style="position:relative; margin-top:10px; margin-left:auto;">
    <div><p>密码:   <input type="password"; style="width:160px"; id=password; name=password></input></p></div>
    <div id=pwd></div>
    </div>
    <div style="margin-top:10px;margin-left:50px;"><input style="width:70px" type="submit"; value="登录" onclick="test()"></input></div>
    </div>
    </a></body></html>
      

  7.   

    <html>
    <head>
    </head>
    <body><a>
    <script type="text/javascript">
    function $(id){
    return document.getElementById(id);
    }
    window.onload = function(){$('username').focus();$('username').onkeydown = function(){
    if(event.keyCode == 13){
    $('password').select();
    }
    }
    $('password').onkeydown = function(){
    if(event.keyCode == 13){
    chklg();
    window.open('http://www.sohu.com','found','width=300,height=200')
    }
    }
    } //<== 这里少了闭弧function test(){
    chklog(); //<== 这个函数未定义
    window.open('http://192.168.0.253/search/search.html','found','width=300,height=200')
    }function chklg(){
    if($('username').value != "sa"){
    alert('用户名输入错误');
    $('username').select();
    return false;
    }
    if($('username').value == ''){
    alert('请输入用户名!');
    $('username').focus();
    return false;
    }
    if($('password').value != "sa"){
    alert('密码输入错误');
    $('password').select();
    return false;
    }
    if($('password').value == ''){
    alert('请输入密码!');
    $('password').focus();
    return false;
    }
    }</script>
    <div style=" border:1px #ddd solid; overflow:hidden;text-align:center; float:center; margin:100px;">
    <div style="textalign=center; margin-top:10px; margin-left:auto">
    <div><p>用户名:<input type=text; style="width:160px"; id="username"; name="username"></input></p></div><div id=usr></div>
    </div>
    <div style="position:relative; margin-top:10px; margin-left:auto;">
    <div><p>密码: <input type="password"; style="width:160px"; id=password; name=password></input></p></div>
    <div id=pwd></div>
    </div>
    <div style="margin-top:10px;margin-left:50px;"><input style="width:70px" type="submit"; value="登录" onclick="test()"></input></div>
    </div>
    </a></body></html>
      

  8.   

    function test() 方法放在最后
      

  9.   

    chklog(); //<== 这个函数未定义
    window.open('http://192.168.0.253/search/search.html','found','width=300,height=200')
    }function chklg(){   ----> 这里不是有函数体么? 不好意思初学编程不是很懂,该怎么定义函数?
    if($('username').value != "sa"){
      

  10.   

    不好意思, 浏览器有错误提示? 我怎么没看到,在哪里能看到,因为初学,很多不懂的还有function test()放在最后的意思是放在哪个位置?谢谢
      

  11.   

    放在function chklg() 下面
      

  12.   

    chklog(); //<== 这个函数未定义
    window.open('http://192.168.0.253/search/search.html','found','width=300,height=200')
    }function chklg(){ ----> 这里不是有函数体么? 不好意思初学编程不是很懂,该怎么定义函数?
    if($('username').value != "sa"){睁大眼睛看看,是一样吗?
    chklog
    chklg
      

  13.   

    function chklg()  chklog(); //<== 这个函数未定义
    多个o