简体   繁体   English

JS function onclick 不工作。 它没有调用验证

[英]JS function onclick is not working. It is not calling validations

Below is function and it is not working.下面是 function,它不工作。 Maybe some small bug in the code which I am unable to identify.也许我无法识别代码中的一些小错误。

<!DOCTYPE html>
<script>
function check(){
  firstname=document.getelementById("firstname").value;
  lastname=document.getelementById("lastname").value;
  if(lastname==""){
    alert('please enter lastname');
  }
}
</script>

<form action="" method="post" onSubmit="check();">
            <input type="text" id="firstname" name="firstname" ><br>
            <input type="text" id="lastname" name="lastname" ><br>
            <input type="submit"  value="submit">
</form>
</body>
</html>

GetElementById, element with a capital E GetElementById,大写 E 的元素
JavaScript is case sensive. JavaScript 区分大小写。 TIP: use developer options in your browser.提示:在浏览器中使用开发者选项。 They usualy give an error message.他们通常会给出错误信息。

 <.DOCTYPE html> <script> function check(){ firstname=document.getElementById("firstname");value. lastname=document.getElementById("lastname");value; if(lastname==""){ alert('please enter lastname'); } } </script> <form action="" method="post" onSubmit="check() "> <input type="text" id="firstname" name="firstname" ><br> <input type="text" id="lastname" name="lastname" ><br> <input type="submit" value="submit"> </form> </body> </html>

The bug in the code: case sensitive getelEmentById.代码中的错误:getelElementById 区分大小写。 Please below code and test it请在下面的代码和测试它

function check(){
  firstname=document.getElementById("firstname").value;
  lastname=document.getElementById("lastname").value;
  if(lastname==""){
    alert('please enter lastname');
  }
}

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

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