Home > Article > Web Front-end > How to compare sizes in jquery? 5 methods introduced
In jQuery, there are many ways to compare sizes. The following are some of the methods:
var num1 = parseFloat($('#input1').val()); var num2 = parseFloat($('#input2').val()); if(num1 > num2){ //... }
var num1 = $('#input1').val(); var num2 = $('#input2').val(); num1 > num2 ? console.log(num1 + " > " + num2) : console.log(num1 + " < " + num2);
var num1 = parseFloat($('#input1').val()); var num2 = parseFloat($('#input2').val()); if(num1 > num2){ console.log(num1 + " > " + num2); } else { console.log(num1 + " < " + num2); }
var numArr = []; $('input').each(function(index){ numArr.push(parseFloat($(this).val())); }); var max = Math.max.apply(null, numArr); var min = Math.min.apply(null, numArr); console.log("Max:" + max + " Min:" + min);
var numArr = $('input').map(function(index, elem){ return parseFloat($(elem).val()); }); var max = Math.max.apply(null, numArr); var min = Math.min.apply(null, numArr); console.log("Max:" + max + " Min:" + min);
To sum up, there are many ways to compare sizes in jQuery, and developers can choose the method that suits them best according to the specific situation.
The above is the detailed content of How to compare sizes in jquery? 5 methods introduced. For more information, please follow other related articles on the PHP Chinese website!