javascript
  • jquery
  • 2013-05-19 72 views 0 likes 
    0

    我希望有人能帮助我的项目学校。我有这样一段HTML代码:我的Javascript不能在localhost工作XAMPP

    <!DOCTYPE html> 
    <html> 
        <head> 
         <title>Tambah Guru</title> 
         <link rel="stylesheet" type="text/css" href="stylesheet.css"/> 
         <script type='text/javascript' src='script.js' charset="utf-8"></script> 
        </head> 
    <body> 
    
    Email: <input type='text' id='txtemail' /> 
    
    <input type='submit' value='Simpan' id='validateemail' /> 
    </body> 
    </html> 
    

    然后我的JavaScript代码:

    $(document).ready(function(e) { 
        $('#validateemail').click(function() { 
         var sEmail = $('#txtemail').val(); 
         if ($.trim(sEmail).length == 0) { 
          alert('Please enter valid email address'); 
          e.preventDefault(); 
         } 
         if (validateEmail(sEmail)) { 
          alert('Email is valid'); 
         } else { 
          alert('Invalid Email Address'); 
          e.preventDefault(); 
         } 
        }); 
    }); 
    
    function validateEmail(sEmail) { 
        var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; 
        if (filter.test(sEmail)) { 
         return true; 
        } else { 
         return false; 
        } 
    } 
    
    当我运行它

    ,这是行不通的。我不知道为什么。有人可以帮我吗?

    +0

    哪部分*不起作用*? – Joseph

    +4

    对于一个他不包括jquery。 – 2013-05-19 05:12:38

    +3

    按照[jQuery.com的说明](http://learn.jquery.com/about-jquery/how-jquery-works/)在您的页面上添加jquery.js。如果仍不起作用,请澄清“不工作”的含义:指定所需的行为和实际行为。 – nnnnnn

    回答

    6

    您没有像上述评论中那样添加jQuery。把这一行放在html中的脚本行上面

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
    

    然后你没有传入要处理的事件对象。在下面的代码中传递click处理程序中的事件对象。您正在传递document.ready中的事件处理程序,这不是您需要的。

    $(document).ready(function() { 
        $('#validateemail').click(function(e) { 
         var sEmail = $('#txtemail').val(); 
         if ($.trim(sEmail).length == 0) { 
          alert('Please enter valid email address'); 
          e.preventDefault(); 
         } 
         if (validateEmail(sEmail)) { 
          alert('Email is valid'); 
         } 
         else { 
          alert('Invalid Email Address'); 
          e.preventDefault(); 
         } 
        }); 
    }); 
    
    function validateEmail(sEmail) { 
        var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; 
        if (filter.test(sEmail)) { 
         return true; 
        } 
        else { 
         return false; 
        } 
    } 
    
    +1

    +1感谢您的解释。向你学习很多人:) –

    +0

    thans解释:) –

    1

    您需要包含jquery.js文件,因为它是一个Javascript库。您可以下载,其中包括,或干脆在脚本标签使用这个网址: -

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
    

    这是一个更好的选择,因为它具有很多优点类似延迟的减少,增加的并行性,以及更好的缓存。

    +0

    atleast复制粘贴其他答案时复制。我没有在http后添加冒号,现在编辑它。编辑你的答案包括它 – theshadowmonkey

    +0

    @shadowmonkey这是可以理解的。不是吗? –

    +0

    的确如此。但是当你发布一个答案时,至少考虑修改它,因为不是每个人都具有相同的技能水平,或者有时你和我几个小时呆在一起傻了:)只是说。 – theshadowmonkey

    相关问题