找到具有最大值-jquery的文本字段

时间:2011-09-02 10:28:45

标签: javascript jquery

我在5行上有5个输入文本字段。所有5个文本字段都是可选的。我想获取具有最大值的文本字段的id

<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>

我已经完成了验证,输入字段只会保留数字,值应按升序排列。

3 个答案:

答案 0 :(得分:1)

可能有点复杂,但效果很好:

var id = $(".number").filter(function() { return $(this).val(); }).last().attr("id");

它获取所有输入,过滤那些有值并返回最后一个的id。

答案 1 :(得分:0)

function getInput() {
    var lastInputId = null;

    $('g:textField').each(function(i, item) {
        item.value && (lastInputId = item.id);
    });
    return lastInputId;
}

答案 2 :(得分:0)

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;
}