find the textfield which has got maximum value-jquery
I have 5 input text fields on 5 rows . where all 5 textfields are optional .i want to get the id of the text field which ha开发者_运维技巧s got maximum value
<div class="dt_distance_slab">
<g:textField class ="number distanceSlab1" name="distanceSlabCost1" id = "distanceSlabCost1" value=""/>
</div>
<div class="dt_distance_slab">
<g:textField class ="number distanceSlab2" name="distanceSlabCost2" id = "distanceSlabCost2" value=""/>
</div>
<div class="dt_distance_slab">
<g:textField class ="number distanceSlab3" name="distanceSlabCost3" id = "distanceSlabCost3" value=""/>
</div>
<div class="dt_distance_slab">
<g:textField class ="number distanceSlab4" name="distanceSlabCost4" id = "distanceSlabCost4" value=""/>
</div>
<div class="dt_distance_slab">
<g:textField class ="number distanceSlab5" name="distanceSlabCost5" id = "distanceSlabCost5" value=""/>
</div>
i have done a validation that input fields will aceept only numbers and values should be in ascending order.
Maybe a bit complicated but it works good:
var id = $(".number").filter(function() { return $(this).val(); }).last().attr("id");
It gets all the inputs, filters those that have value and returns an id of the last one.
function determineLastWithValue = function() {
var lastWithValue, iset = $('.dt_distance_slab > input'), i;
for (i = 0; i < iset.length; i++) {
if (iset[i].value == '') {
break;
}
lastWithValue = iset[i];
}
return lastWithValue;
}
function getInput() {
var lastInputId = null;
$('g:textField').each(function(i, item) {
item.value && (lastInputId = item.id);
});
return lastInputId;
}
精彩评论