外部JavaScript文件未运行

时间:2017-11-14 14:47:51

标签: javascript html google-chrome

我已尝试在Chrome上运行以下电子邮件列表应用程序,但似乎无法执行JavaScript。 HTML工作正常。有关此应用程序的任何建议或最佳实践吗?我也包含了外部JavaScript文件:

<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8”>
<title>Join E-mail List</title>
<script src = "index.js"></script>
</head>
<body>
<main>
<h1> Please join our email list</h1>
<form id=”email_form” name=”email_form”
action=”join.html” method=”get”>
<label for=”email_address1”>E-mail Address:</label>
<input type=”text” id=”email_address1” name=”email_address1”>
<span id=”email_address1_error”>*</span><br>

<label for=”email_address2”>Re-enter E-mail Address:</label>
<input type=”text” id=”email_address2” name=”email_address2”>
<span id=”email_address2_error”>*</span><br>

<label for="first_name">Name:</label>
<input type="text" id="first_name" name="first_name">
<span id="first_name_error">*</span><br>

<label>&nbsp;</label>
<input type="button" id="join_list" value="Join Our List">
</form>
</main>
</body>
</html>
enter code here

外部JavaScript文件(index.js)

/* This application validates a user's entry for joining our E-mail 
    list
*/

<script>
var $ = function(id) {
return document.getElementById(id);

//This function gets and validates the user entries

var joinList = function() {
var emailAddress1 = $("email_address1").value;
var emailAddress1 = $("email_address2").value;
var firstName = $("first_name").value;
var isValid = true;

//This validates the first entry

if ("email_address1 = "") {
$ ("email_address1_error").firstChild.nodeValue = "This is 
required.";
isValid = false;

if (email_address2 = "") {
$ ("email_address2_error").firstChild.nodeValue = "This is 
required.";
isValid = false;

if ("first_name = "") {
$ ("first_name_error").firstChild.nodeValue = "First name is 
required.";

}
}
}
</script>

1 个答案:

答案 0 :(得分:1)

index.js的内容应该是固定的:

var $ = function(id) {
return document.getElementById(id);

//This function gets and validates the user entries

var joinList = function() {
var emailAddress1 = $("email_address1").value;
var emailAddress1 = $("email_address2").value;
var firstName = $("first_name").value;
var isValid = true;

//This validates the first entry

if ("email_address1" == "") {
$ ("email_address1_error").firstChild.nodeValue = "This is required.";
isValid = false;

if ("email_address2" == "") {
$ ("email_address2_error").firstChild.nodeValue = "This is required.";
isValid = false;

if ("first_name" == "") {
$ ("first_name_error").firstChild.nodeValue = "First name is required.";

}
}
}
}