Jquery选择Value大于Zero的最后一个字段

时间:2012-03-22 08:25:45

标签: javascript jquery

大家好我有像

这样的字段
 <div class="dlt_distance_slab">
   <div class="dl_distance_slab">
       <label for="upto"><g:message code="rule.upto.label" default="Upto" /></label>
   </div>
   <div class="dt_distance_slab_show">
        <g:formatNumber number="${ruleInstance?.distanceSlabLimit1}" type="number" maxFractionDigits="2" class="distanceSlablimit"  /> Km
   </div>
 </div>
 <div class="dlt_distance_slab">
    <div class="dl_distance_slab">
         <label for="upto"><g:message code="rule.upto.label" default="Upto" /></label>
    </div>
    <div class="dt_distance_slab_show">
      <g:formatNumber number="${ruleInstance?.distanceSlabLimit2}" type="number" maxFractionDigits="2" class="distanceSlablimit" /> Km
    </div>      
 </div>
 <div class="dlt_distance_slab">
      <div class="dl_distance_slab">
       <label for="upto"><g:message code="rule.upto.label" default="Upto" /></label>
  </div>
  <div class="dt_distance_slab_show">
       <g:formatNumber number="${ruleInstance?.distanceSlabLimit3}" type="number" maxFractionDigits="2" class="distanceSlablimit" /> Km
  </div>        
</div>

我想选择值为&gt;的最后一个formatNumber字段0(所有formatNumber Fiels都有相同的class =“distanceSlablimit”

如何使用jquery

2 个答案:

答案 0 :(得分:1)

首先,您可以过滤所选元素using a function as a parameter for .filter()。然后,您只需使用.last()

parseInt()用于将值转换为数字。确保始终指定第二个参数(基数),否则您会注意到一些"strange" behaviour。例如,'010'将被转换为数字8,因为在Javascript中,起始零表示八进制数。

$('.distanceSlablimit').filter(function () {
    //replace val() with text() if the elements are not form elements
    return parseInt($(this).val(), 10) > 0;
}).last();

jsFiddle Demo / and another with .text()

答案 1 :(得分:-1)

您可以使用filter()last();

$('.distanceSlablimit').filter(function() {
  return parseInt($(this).val() || $(this).text()) > 0); // Checking val() and text() as the type of element is unknown.
}).last();