How do I get smallest value开发者_StackOverflow社区 of the textbox using jquery? rows means trs which contain multiple tds & tds in turn contains textboxes I want to find the smallest value of each tr
$.each(rows, function () {
$(this).find("input[type='text']") //find smallest value of the textbox
});
how can I achieve this?
You can build an array of the smallest <input>
values in each <tr>
using .map()
. Assuming you want to interpret each value as a number:
var minValues = $(rows).map(function ()
{
var rowVals = $(this).find('input:text').map(function ()
{
return Number($(this).val());
}).get();
return Math.min.apply(null, rowVals);
}).get();
API docs:
.map()
:text
selector.val()
Math.min
Function.apply
If you don't need to use the array as a whole, and just iterate over the minimum values:
$(rows).each(function ()
{
var rowVals = $(this).find('input:text').map(function ()
{
return Number($(this).val());
}).get();
var min = Math.min.apply(null, rowVals);
// do stuff with min
})
You're almost there. Here's what I did:
$.each(rows, function () {
var sorted = $(this).find("input[type='text']").sort(
function(a, b) { return a.value - b.value }
);
var lowest = sorted[0].value;
});
精彩评论