jQuery 我的 Javascript 在 localhost XAMPP 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16631659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:29:55  来源:igfitidea点击:

My Javascript is not working in localhost XAMPP

javascriptjquery

提问by Vinra Gunanta Pandia

I hope someone can help my project school. I have a piece of HTML code like this:

我希望有人可以帮助我的项目学校。我有一段这样的 HTML 代码:

<!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>

And then my Javascript code is:

然后我的 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;
    }
}

when I run it, it does not work. I don't know why. Can anybody help me please?

当我运行它时,它不起作用。我不知道为什么。有人可以帮我吗?

回答by theshadowmonkey

You are not adding jQuery as in the above comments. Put this line above your script line in html

您没有像上面的评论那样添加 jQuery。将此行放在 html 中的脚本行上方

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

And then you are not passing in the event object to be handled. Pass the event object in the click handler like in the code below. You are passing the event handler in the document.ready which is not you need.

然后你没有传入要处理的事件对象。在单击处理程序中传递事件对象,如下面的代码所示。您正在传递 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;
    }
}

回答by Vivek Sadh

You need to include jquery.js file as it is a Javascript library. You can either download and include it or simply use this url in script tag:-

您需要包含 jquery.js 文件,因为它是一个 Javascript 库。您可以下载并包含它,也可以简单地在脚本标签中使用此网址:-

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

This is a better option as it has many advantages like decreased latency, increased parallelism, and better caching.

这是一个更好的选择,因为它具有许多优点,例如减少延迟、增加并行度和更好的缓存。