简体   繁体   English

不知道为什么我的验证器不起作用

[英]No idea why my validators aren't working

Hi i am new to html and javascript. 嗨,我是html和javascript的新手。 I encountered a problem which seems correct but it doesn't work. 我遇到了一个看似正确的问题,但是它不起作用。 There must be in problem in the function, which i can't figure it out. 函数中肯定有问题,我无法弄清楚。

 <html> <body> <section> <form name="CombinationWithRep"> <h2>Combination with repetition</h2> N:<input type="text" name="numberOfThings" /> R: <input type="text" name="selection" /> <button onclick="CombinationWithRepfun()">Sub</button> <p id="demo1"></p> </form> <script langauge="javascript"> function CombinationWithRepfun() { var number = document.forms["CombinationWithRep"]["numberOfThings"].value; var selector = document.forms["CombinationWithRep"]["selection"].value; if (number == " ") { alert("pleas typo"); return false; } if (selector == " ") { alert("pleas typo"); return false; } var number1 = number * 1; var selector1 = selector * 1; document.getElementById("demo").innerHTML = Math.pow(number1, selector1); } </script> </section> </body> </html> 

There are few issues in your code: 您的代码中有几个问题:

  1. You are checking the value with a space ( 您正在用空格检查值( ), which I think you didn't wanted. ),我认为您不想要。 You have to check the value with empty string. 您必须使用空字符串检查该值。

  2. You can use event.preventDefault() to prevent the event from further execution if the condition is true. 如果条件为真,则可以使用event.preventDefault()阻止事件进一步执行。

  3. You should execute other code if the condition is false. 如果条件为假,则应执行其他代码。

  4. You also have to use demo1 instead of demo . 您还必须使用demo1而不是demo Try the following code: 尝试以下代码:

 <form name="CombinationWithRep"> <h2>Combination with repetition</h2> N:<input type="text" name="numberOfThings"/> R:<input type="text" name="selection"/> <button type="button" onclick="CombinationWithRepfun(event)">Sub</button> <p id="demo1"></p> </form> <script langauge="javascript"> function CombinationWithRepfun(e) { var number = document.forms["CombinationWithRep"]["numberOfThings"].value; var selector = document.forms["CombinationWithRep"]["selection"].value; if (number.trim() == "" || selector.trim() == "") { alert("pleas typo"); e.preventDefault(); } else { var number1 = number*1; var selector1 = selector*1; document.getElementById("demo1").innerHTML = Math.pow(number1, selector1); } } </script> </section> 

首先,在脚本标记中输入错误(“语言”而不是“ langauge”。)第二,我认为您需要使用parseInt或ParseFloat将数字1和选择器1从字符串转换为数字

Answers are below and here's how I would build that: https://codepen.io/yamato79/pen/YjwzQE hope this helps, have a nice day. 答案在下面,这是我的构建方法: https : //codepen.io/yamato79/pen/YjwzQE希望这对您有所帮助,祝您度过愉快的一天。

  • Add type="button" <button type="button" onclick="CombinationWithRepfun()">Sub</button> attribute so it doesn't submit the form when you click on the button. 添加type =“ button” <button type="button" onclick="CombinationWithRepfun()">Sub</button>属性,以便在单击按钮时不提交表单。 It keeps refreshing the page everytime you click the button because it's submitting the form to the same page. 每次您单击按钮时,它都会刷新页面,因为它将表单提交到同一页面。

  • if (selector == "") if (number == "") Compare it with just quotes. if (selector == "") if (number == "")将其与引号进行比较。 You used " " which is not equal to the input being empty. 您使用的“”不等于输入为空。

  • Change <script langauge="javascript"> to <script type="text/javascript"> <script langauge="javascript">更改为<script type="text/javascript">

  • document.getElementById("demo1").innerHTML = Math.pow(number1, selector1); And lastly, your element ID was incorrect. 最后,您的元素ID不正确。

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

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