【发布时间】:2019-01-05 04:40:11
【问题描述】:
我在 Table 中有 foreach 循环(rails),我想从一个输入文本计算每一行。
我已经尝试创建 jquery,但它只计算表的第一行。
对此的预期输出是,它必须看到从输入文本 keyup 计算的每一行
<input type = "text" class = "share_amount" id = "alldata" >
<table class="table table-borderless table-striped table-earning">
<thead>
<tr>
<th>User</th>
<th>Amount</th>
<th>Share</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<% @userfile.each do |info|%>
<tr class = "itemrow">
<td><%= info.name %></td>
<td class = "price1"><%= info.amount %></td>
<td><%= info.share %></td>
<td class = "result"></td>
</tr>
<%end%>
<script>
$(document).ready(function(){
$("input#alldata").keyup(function() {
var share = this.value;
$('tr.itemrow').each(function () {
$(this).find('.price1').each(function(){
var tdval = $(this).text();
$(".result").text(share * tdval);
});
});
});
});
</script>
【问题讨论】:
-
认为你有太多的
.each()在运行。您在行中寻找“price1”,是否有超过 1 个?看起来不是那样,所以我不会在上面使用.each()。
标签: jquery ruby-on-rails ruby