使用jQuery循环输入字段以找到最高值?

时间:2012-10-30 23:00:47

标签: jquery

我的网页上有许多input[type=text]字段,我想遍历所有字段以查找并返回最高值。

有没有办法用jQuery做到这一点?

感谢您的帮助。

2 个答案:

答案 0 :(得分:10)

这是一个解决方案:

var highest = -Infinity;
$("input[type='text']").each(function() {
    highest = Math.max(highest, parseFloat(this.value));
});
console.log(highest);

这是另一种解决方案:

var highest = $("input[type='text']").map(function() {
    return parseFloat(this.value);
}).get().sort().pop();

console.log(highest);

答案 1 :(得分:4)

使用Math.max功能:

var nums = [];
$("input[type=text]").each( function() { nums.push( $(this).val() ); });
var max = Math.max.apply(Math, nums);
相关问题