简体   繁体   English

javascript给出奇怪的输出

[英]javascript giving weird output

I have this weird problem with javascript. 我对javascript有这个奇怪的问题。 I wrote a code that checks if the text input from the user is good, and after a while the message saying if it is good or not disappears, any suggestions? 我写了一个代码,检查用户输入的文本是否良好,过一会儿消息说该文本是否良好消失,有什么建议吗?

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <ul style="text-align:"right" dir="rtl"> <script type="text/javascript"> function validate() { var ok = true; if (FirstName() == false) { ok = false; } } function FirstName() { document.getElementById("firstName").innerHTML = ""; var firstName = document.getElementById("firstName").value; var length = firstName.length; if (length < 2) { document.getElementById("firstN").innerHTML = "not good"; return false; } else { document.getElementById("firstN").innerHTML = "good"; } return true; } </script> </head> <body> <form id="form1" runat="server" onsubmit="return validate()" action="Default2.aspx"> <div> enter you first name <input type="text" id="firstName" /> <label id="firstN"></label> <br /> <input type="submit" value="send"/> </div> </form> </body> </html> 

Well, 好,

Please remember as you have mentioned action in the form, the control will transferred to the page mentioned in it when "TRUE" is encountered. 请记住,当您在表单中提到动作时,遇到“ TRUE”时,控件将转移到其中提到的页面。 If you still want to see "good", you can remove action. 如果您仍然希望看到“良好”,则可以删除操作。

So to get your code working, you can either change it as below, 因此,要使代码正常工作,您可以按以下方式进行更改,

  <script type="text/javascript"> function validate() { if (FirstName() == false) { return false; } else { return true; } } function FirstName() { document.getElementById("firstName").innerHTML = ""; var firstName = document.getElementById("firstName").value; var length = firstName.length; if (length < 2) { document.getElementById("firstN").innerHTML = "not good"; return false; } else { document.getElementById("firstN").innerHTML = "good"; } return true; } </script> 

OR, 要么,

You can use jQuery Validation, which is way better and less time consuming, 您可以使用jQuery Validation,这是一种更好的方法,并且耗时更少,

jQuery Form Validation example jQuery表单验证示例

jQuery Validate Options jQuery验证选项

The submit action refreshes the page that's why you only see that for a moment. 提交操作会刷新页面,这就是为什么您只看到片刻的原因。 You have to work with async form if you want to stay on that page. 如果要保留在该页面上,则必须使用异步表单。

Read this http://blog.teamtreehouse.com/create-ajax-contact-form 阅读此http://blog.teamtreehouse.com/create-ajax-contact-form

and This https://learn.jquery.com/ajax/ajax-and-forms/ 和这个https://learn.jquery.com/ajax/ajax-and-forms/

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

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