1. Background of the problem
Today, in the project encountered a problem: two input box, one input box value is 8.4, the other is 21.3, compare two input box value, there is 8.4 greater than 21.3
2, problem analysis
First, getting the value in the input box is a string, while the string comparison is based on the ASCII code, since 8 of the ASCII code is larger than the ASCII code of 2, resulting in 8.4 greater than 21.3
3, problem verification
(1) Design source code
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
(2) Operation result
4. Solutions
(1) Converting a string into a numeric value
function Comparedata () { var data1 = document.getElementById ("Inval"). Value;var data2 = document.getElementById (" Typev "). Value;if (number (DATA1) > Number (data2)) { alert (data1+" greater than "+data2);} Else{alert (data1+ "less than equals" +data2);}}
(2) Operation result
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JavaScript string Numeric comparison problem