简体   繁体   English

我怎样才能让 Javascript 显示我的 JavaScript 代码中的错误,为什么我的提交按钮不起作用?

[英]How can I get Javascript to display the errors that are in my JavaScript code and why doesn't my submit button work?

 var $ = function(id) { return document.getElementById(id); } var submitForm = function() { var FirstName = $("firstName").value; var OrderNumber = $("orderNumber").value; var DateOfOrder = $("date_of_order").value; var EmailAddress = $("email_address").value; var isValid = true; if (FirstName !== "Cherry" && FirstName !== "Micheal" && FirstName !== "Sandra" && FirstName !== "Cookie") { $("firstname_error").firstChild.nodeValue = "This person does not exist."; isValid = false; } else { $("firstname_error").firstChild.nodeValue = ""; } if (OrderNumber !== 3134 && OrderNumber !== 4234 && OrderNumber !== 9234 && OrderNumber !== 3566) { $("orderNumber_error").firstChild.nodeValue = "Invalid Order Number."; isValid = false; } else { $("orderNumber_error").firstChild.nodeValue = ""; } if (DateOfOrder !== 12 - 07 - 23 && DateOfOrder !== 15 - 04 - 24 && DateOfOrder !== 16 - 02 - 01 && DateOfOrder !== 14 - 01 - 12) { $("date_of_order_error").firstChild.nodeValue = "Date doesn't exist in system"; isValid = false; } else { $("date_of_order_error").firstChild.nodeValue = ""; } if (EmailAddress !== "cherryjackson@gmail.com" && EmailAddress !== "michealroberts@yahoo.com" && EmailAddress !== "sandrabell@hotmail.com" && EmailAddress !== "cookiedanny@outlook.com") { $("email_address_error").firstChild.nodeValue = "The email doesn't exist"; isValid = false; } else { $("email_address_error").firstChild.nodeValue = ""; } if (isValid) { //submit the form if all entries are valid $("cookie_form").submit(); } } window.onload = function() { $("form_submission").onclick = submitForm; $("email_address").focus(); }
 body { background-color: #FBFBE8; } /* Tells HTML5 to find the font-type-face that my OS has and then use that for heading 1 and also centers the first heading */ h1 { font-family: Arial, Helvetica, sans-serif; text-align: center; } /* Tells HTML5 to use any of the font-types for my first paragraph in HTML source file if one is not available. Also clears some white space from the left margin of the paragraph and finally tells it to give that paragraph a size of 20 pixels. */ p { font-family: Arial, Helvetica, sans-serif; padding: 20px; font-size: 20px; } label { float: left; width: 11em; text-align: right; font-family: Arial, Helvetica, sans-serif; color: #800000; } input { margin-left: 1em; margin-bottom: .5em; } span { color: red; } .field_set_1 { border-color: purple; border-style: solid; } #form_submission { background-color: black; color: white; } legend { font-family: Arial, Helvetica, sans-serif; color: blue; } /* All of the classes are just for positioning and floating the four same images around the form input information */ .Wrap1 { float: right; margin: 40px; width: 200px; height: 200px; } .Wrap2 { float: left; margin: 40px; width: 200px; height: 200px; } .clearfix { clear: both; }
 <h1>Cookie Order Form</h1> <p>This form is a cookie order form for customers that purchased cookies from Daron's Cookies Company and the following below must be filled out in order for each customer to receive a final message that tells them when their order will be ready.</p> <IMG class="Wrap1" SRC="cookie.gif" alt="cookie"> <IMG class="Wrap2" SRC="cookie.gif" alt="cookie2"> <!--The customer will be sent to the HTML page named "submit form.html" after they click the "Submit this Form" button. The code below does this. --> <div> <form id="cookie_form" name="cookie_form" action="submit form.html" method="get"> <fieldset class="field_set_1"> <!-- Below sets the title of the form--> <legend>Customer Order Form Information:</legend> <!-- Creates the first left label to specify what should be placed in the text box the the right of the label. The rest below does the same.--> <label for="firstName">First Name:</label> <input type="text" id="firstName" name="firstName"> <span id="firstname_error">*</span> <br> <label for="orderNumber">Order Number:</label> <input type="text" id="orderNumber" name="orderNumber"> <span id="orderNumber_error">*</span> <br> <label for="date_of_order">Date of Order:</label> <input type="text" id="date_of_order" name="date_of_order"> <span id="date_of_order_error">*</span> <br> <label for="email_address">Email Address:</label> <input type="text" id="email_address" name="email_address"> <span id="email_address_error">*</span> <br> <label>&nbsp;</label> <input type="button" id="form_submission" value="Submit this Form"> </fieldset> </form> </div> <div class="clearfix"> </div> <IMG class="Wrap1" SRC="cookie.gif" alt="cookie"> <IMG class="Wrap2" SRC="cookie.gif" alt="cookie2">

I have been working on this for hours.我已经为此工作了几个小时。 I am trying to figure out why won't my JavaScript code won't display the error message in the form when customers enter invalid order Numbers, incorrect order dates, or names that system can't detect.我想弄清楚当客户输入无效的订单号、错误的订单日期或系统无法检测到的名称时,为什么我的 JavaScript 代码不会在表单中显示错误消息。 etc. Can someone pinpoint in the right direction of why my code doesn't work for the form and why won't it go to the next page when I click on the 'submit this form' button.等等。有人可以指出为什么我的代码对表单不起作用的正确方向,为什么当我单击“提交此表单”按钮时它不会转到下一页。

The value coming from your input elements doesn't match to orderNumber and date of birth.来自输入元素的值与 orderNumber 和出生日期不匹配。 puting them in quotes (inside condition check) will solve the issue.将它们放在引号中(内部条件检查)将解决问题。

if(DateOfOrder !== "12-07-23" && DateOfOrder !== "15-04-24" &&
   DateOfOrder !== "16-02-01" &&  DateOfOrder !== "14-01-12")
{....}

Working Example工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM