.each()两个不同的类

时间:2018-02-26 03:20:25

标签: javascript jquery

我试图将2个输入与行中不同的类进行比较,但列中的类相同。我试过.each,但我有一个错误。在指定amount_remaining


未捕获的TypeError:$('。amount_remaining')[i] .val

var i = 0;
$(document).on('click','#sub',function(){
  $('.amount_paid').each(function(){
var val_paid = $(this).val();
  if(val_paid > $('.amount_remaining')[i].val())
  {
     console.log("Something is Greater Than");
  }
  else
  {
     console.log("Something is Less Than");
  }

  i++;
});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
  <td><input class = 'amount_paid' type = 'number'></td>
  <td><input class = 'amount_remaining' type = 'number'></td>
</tr>
<tr>
  <td><input class = 'amount_paid' type = 'number'></td>
  <td><input class = 'amount_remaining' type = 'number'></td>
</tr>
<tr>
  <td><input class = 'amount_paid' type = 'number'></td>
  <td><input class = 'amount_remaining' type = 'number'></td>
</tr>
</table>

<button id = 'sub'>SUBMIT</button>

3 个答案:

答案 0 :(得分:2)

由于您从class RecipeListItem extends Component { constructor() { super(); this.state = { valueButton: false, }; this.handleFavorites = this.handleFavorites.bind(this); } // rest... 元素列表中访问一个元素的方式,您收到错误。 您应该使用$('.amount_remaining')$('.amount_remaining')[i].value

请参阅下面的代码。

$('.amount_remaining').eq(i).val()
var i = 0;
$(document).on('click','#sub',function(){
  $('.amount_paid').each(function(){
var val_paid = $(this).val();
  if(val_paid > $('.amount_remaining').eq(i).val())
  {
     console.log("Something is Greater Than");
  }
  else
  {
     console.log("Something is Less Than");
  }

  i++;
});

});

答案 1 :(得分:1)

以下是您的工作样本问题。

&#13;
&#13;
var i = 0;
$(document).on('click','#sub',function(){
var lstAmtpaid =   $('.amount_paid');
var lstAmtremaining = $('.amount_remaining');

  var inpitLength = lstAmtpaid.length;  
  while (i < inpitLength)
  {
  console.log($($('.amount_paid')[0]).val())
  
  if($(lstAmtpaid[i]).val() > $(lstAmtremaining[i]).val())
  {
     console.log("Something is Greater Than");
  }
  else
  {
     console.log("Something is Less Than");
  }

  i++;
  }

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
  <td><input class = 'amount_paid' type = 'number'></td>
  <td><input class = 'amount_remaining' type = 'number'></td>
</tr>
<tr>
  <td><input class = 'amount_paid' type = 'number'></td>
  <td><input class = 'amount_remaining' type = 'number'></td>
</tr>
<tr>
  <td><input class = 'amount_paid' type = 'number'></td>
  <td><input class = 'amount_remaining' type = 'number'></td>
</tr>
</table>

<button id = 'sub'>SUBMIT</button>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

在这里,您可以使用.get(index)从jQuery对象获取元素的索引。