简体   繁体   English

if.IsInteger()在if else语句中不起作用

[英]Number.IsInteger() in if else statement not working

 function bereken() { input1 = document.getElementById('input1').value; input2 = document.getElementById('input2').value; input1Valid = Number.isInteger(document.getElementById('input1')); input2Valid = Number.isInteger(document.getElementById('input2')); if (Number.isInteger(input1) && Number.isInteger(input2)) { alert('De getallen zijn allebei hele getallen') } } 
 <!DOCTYPE html> <html> <head> <script src='code.js'></script> </head> <body> <center> <h1>GGD calculator</h1> <p>Reken de Grote Gemene Deler uit van <input type='number' id='input1'> en <input type='number' id='input2'>.</p> <br> <input type='button' value='Bereken!' onClick='bereken()'> </center> </body> </html> 

I was practising JavaScript and I came across this issue, can anybody help me? 我正在练习JavaScript,我遇到了这个问题,任何人都可以帮助我吗?

I want it to alert my message when both numbers entered are integers, why is this not working? 当输入的两个数字都是整数时,我希望它提醒我的信息,为什么这不起作用?

The value of the #num element is returned as a string as you can see in the console. 正如您在控制台中看到的那样,#num元素的值将作为字符串返回。 Just revert it to a number using + sign. 只需使用+号将其还原为数字即可。

 function bereken() { input1 = document.getElementById('input1').value; input2 = document.getElementById('input2').value; if (Number.isInteger(+input1) && Number.isInteger(+input2)) { alert('De getallen zijn allebei hele getallen') } } 
 <!DOCTYPE html> <html> <head> <script src='code.js'></script> </head> <body> <center> <h1>GGD calculator</h1> <p>Reken de Grote Gemene Deler uit van <input type='number' id='input1'> en <input type='number' id='input2'>.</p> <br> <input type='button' value='Bereken!' onClick='bereken()'> </center> </body> </html> 

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

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