简体   繁体   English

此脚本有什么问题,为什么我没有出现错误?

[英]What's wrong with this script and why am I getting no error?

So I'm relatively new to JS and decided to practice a bit. 因此,我是JS的新手,因此决定练习一下。 I wanted to make a webpage to select how many products you want to buy, with two buttons a plus and a minus. 我想制作一个网页来选择要购买的产品数量,并带有两个按钮(加号和减号)。 The program was supposed to add 1 to an input or take 1 depending on what button was impressed. 该程序应该将1加到输入或取1,具体取决于所按下的按钮。 image of webpage 网页图片

So that worked fine but then I added an if statement to make sure the user didn't enter above 4 tickets or lower than 0 tickets. 这样工作正常,但随后我添加了if语句,以确保用户输入的票证不高于4张票证或低于0张票证。 This produced nothing and in developers tools on chrome it just flashes an error but doesn't keep it there. 这什么也没产生,在chrome上的开发人员工具中,它只会闪烁一个错误,但不会一直存在。 Any help would be appreciated. 任何帮助,将不胜感激。

 <!DOCTYPE html> <html> <head> <style> .myButton { background-color:#44c767; -moz-border-radius:28px; -webkit-border-radius:28px; border-radius:28px; border:1px solid #18ab29; display:inline-block; cursor:pointer; color:#ffffff; font-family:Arial; font-size:17px; padding:16px 31px; text-decoration:none; text-shadow:0px 1px 0px #2f6627; } .myButton:hover { background-color:#5cbf2a; } .myButton:active { position:relative; top:1px; } </style> <script> function plus(){ document.getElementById("htop").value=Number(document.getElementById("htop").value)+1; } function minus(){ document.getElementById("htop").value=Number(document.getElementById("htop").value)-1; } function validate(){ if(Number(document.getElementById("htop").value)<=0){ alert("You can not submit less than NULL tickets"); return false; } else if(Number(document.getElementById("htop").value)>4){ alert("you can not buy more than 4 tickets at once"); return false; } else{ alert("you may proceed"); return true; } } </script> </head> <body > <form> <button id="plus" class="myButton" onclick="plus()">+</button> <button id="minus" class="myButton" onclick="minus()">-</button> <input type="text" id="htop" value="0" /> <br> <input type="submit" value="Submit" onclick="validate()"> </form> </body> </html> 

I believe that with what you are trying to do, you should not use form tags. 我认为,在尝试执行操作时,不应使用表单标签。 Try removing those. 尝试删除那些。

<body >
    <button id="plus" class="myButton" onclick="plus()">+</button>
    <button id="minus" class="myButton" onclick="minus()">-</button>
    <input type="text" id="htop" value="0" />
    <br>
    <input type="submit" value="Submit" onclick="validate()">


</body>

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

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