简体   繁体   中英

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?

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. 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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