简体   繁体   English

javascript简单验证不起作用?

[英]javascript simple validation not working?

does anyone know why this simple javascript isn't working? 有谁知道为什么这个简单的JavaScript不起作用? it should change the css of the class "t1" from display:none to display: inline to make it appear when the input is empty onSubmit 它应该从display:none更改类“t1”的css来显示:内联使它在输入为空时出现onSubmit

I just can't understand why it doesnt work? 我只是无法理解为什么它不起作用?

Thank you so much if your can find where the problem is (btw i want to keep this in pure javascript) 非常感谢你,如果你能找到问题所在(顺便说一句,我想在纯JavaScript中保留这个)

Javascript: 使用Javascript:

function validate () {

if( document.quote.firstname.value == "" )

{document.getElementByClassName('t1').style = 'display: inline;';
   }

}   

HTML: HTML:

<form name="quote" method="post" action="" onSubmit="validate();">

<fieldset>
<legend>Contact Information</legend>

<div>
<label>*First Name:</label><span class="t1">Please enter your name</span>
<input name="firstname" type="text"/>
</div>

</fieldset>

<div id="f-submit">
<input name="Submit" value="Submit" type="submit"/>
</div>

</form>

CSS: CSS:

.t1{
display: none;
font-size:13px;
color: #F33;
text-align: right;
}   

There is no document.getElementByClassName . 没有document.getElementByClassName Do you mean, getElementsByClassName ? 你的意思是, getElementsByClassName You should also set the display style directly, not through the style attribute. 您还应该直接设置display样式,而不是通过样式属性。 Also, if you need to cancel the form submit, you have to return validate() on the submit, and return false when you want to cancel. 此外,如果您需要取消表单提交,则必须在提交时返回validate(),并在要取消时返回false。 I put it in the fiddle as well. 我也把它放在小提琴里。

I've made a jsFiddle for you as well: http://jsfiddle.net/rgthree/g4ZvA/2/ 我也为你做了一个jsFiddle: http//jsfiddle.net/rgthree/g4ZvA/2/

<form name="quote" method="post" action="" onSubmit="return validate();">

JS: JS:

function validate () {
  if( document.quote.firstname.value == "" ){
    document.getElementsByClassName('t1')[0].style.display = 'inline';
    return false;  // Return false will cancel the submit, causing the page to not load the form action action
  }
  return true;
}

Suggestion 1: Put ID attributes in the tags so you can access them easier 建议1:在标签中放置ID属性,以便您更轻松地访问它们

Suggestion 2: Make the onsubmit attribute be onsubmit="return validate()" 建议2:使onsubmit属性为onsubmit="return validate()"

Suggestion 3: getElementByClassName doesn't exist. 建议3:getElementByClassName不存在。 getElementsByClassName returns an array, so you have to pick which one, or loop through them. getElementsByClassName返回一个数组,因此你必须选择哪一个,或者循环它们。 IE, document.getElementsByClassName('t1')[0] IE, document.getElementsByClassName('t1')[0]

Suggestion 4: Your validate function needs to return false if you want the form to not submit, and true if it should submit. 建议4:如果您希望表单不提交,则验证功能需要返回false,如果要提交,则返回true。

Javascript: 使用Javascript:

function validate () {

   if( document.getElementById("firstname").value == "" || document.getElementById("firstname").value == null )
    {
    document.getElementsByClassName('t1')[0].setAttribute('style','display: inline;');
    return false;
   }
  return true;
}

HTML: HTML:

<form name="quote" method="post" action="" onSubmit="return validate()">

<fieldset>
<legend>Contact Information</legend>

<div>
<label>*First Name:</label><span class="t1">Please enter your name</span>
<input id="firstname" name="firstname" type="text"/>
</div>

</fieldset>

<div id="f-submit">
<input name="Submit" value="Submit" type="submit"/>
</div>

</form>

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

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