为每行中的每个td添加唯一的类
Add unique class to each td in each row jquery/coffeescript
我试图添加一个唯一的类(使用行索引)到每一行的每个td。表行是由用户动态生成的,我只需要在页面加载时格式化这些行。我不太擅长javascript,所以我可能做错了。
表: <table class="inventory">
<thead>
<tr>
<th class="invoice_th"><span>Item <br /><%= link_to "Create New Item", new_item_path, target: "_blank" %></span></th>
<th class="invoice_th"><span>Description</span></th>
<th class="invoice_th"><span>Unit Cost</span></th>
<th class="invoice_th"><span>Quantity</span></th>
<th class="invoice_th"><span>Price</span></th>
</tr>
</thead>
<tbody>
<tr class="itablerow">
<td class="invoice_td"><%= link_to '#', class: "remove_fields cut btn btn-danger" do %>
<i class="fa fa-trash"></i>
<% end %><span id="item">
<%= f.select :name, options_from_collection_for_select(@items, 'name', 'name', f.object.name), {include_blank: "Select Items"}, id: "sel2", class: "select_box" %></span>
</td>
<td class="invoice_td"><span id="description"><%= f.text_field :description, placeholder: "Description", class: "form-control" %></span></td>
<td class="invoice_td"><span id="unit_cost"><%= text_field_tag "unit_cost", f.object.unit_cost, data: {autonumeric: true, aSign: 'USD'}, placeholder: "0.00", class: "form-control" %><%= f.hidden_field :unit_cost, :value => f.object.unit_cost %></span></td>
<td class="invoice_td"><span id="quantity"><%= f.number_field :quantity, placeholder: "0", class: "form-control" %></span></td>
<td class="invoice_td"> <%= text_field_tag "total", f.object.total, data: {autonumeric: true, aSign: 'USD'}, placeholder: "0.00", class: "form-control" %><%= f.hidden_field :total %></td>
</tr>
</tbody>
</table>
我想要完成的是为每行中的每个td添加一个独特的类,这样我就可以很容易地稍后获取它并将单位成本和总数正确地格式化为货币。
这是我正在使用的coffeescript:
$(document).ready ->
prices = []
$('.inventory > tbody > tr').each ->
activeRow = $(this).index()
$('td:nth-child(1) select').addClass 'sel' + activeRow
$('.sel' + activeRow).select2()
$('td:nth-child(2) input').addClass 'description_' + activeRow
$('td:nth-child(3) input:text').addClass 'unit_cost_' + activeRow
$('td:nth-child(3) input:hidden').addClass 'unit_cost_hidden_' + activeRow
$('td:nth-child(4) input').addClass 'quantity_' + activeRow
$('td:nth-child(5) input:text').addClass 'total_' + activeRow
$('td:nth-child(5) input:hidden').addClass 'total_hidden_' + activeRow
quantity = $('td:nth-child(4) input').val()
unit_cost = $('td:nth-child(3) input').val()
fixedUnitcost = accounting.unformat(unit_cost) / 100
total = parseInt(fixedUnitcost, 10) * parseInt(quantity, 10)
$('td:nth-child(3) input:text').val accounting.formatMoney(fixedUnitcost)
$('td:nth-child(5) input:text').val accounting.formatMoney(total)
$('td:nth-child(5) input:hidden').val total
$('td:nth-child(3) input:hidden').val accounting.unformat(unit_cost)
price = accounting.unformat($(this).find('td:last input').val())
prices.push price
sum = prices.reduce(((pv, cv) ->
pv + cv
), 0)
$('.shown_total').text accounting.formatMoney(sum)
totalDue = $('.shown_total').text()
amountPaid = $('.amount_paid').text()
balanceDue = accounting.unformat(totalDue) - accounting.unformat(amountPaid)
$('.balance_due').text accounting.formatMoney(balanceDue)
$('.total').val sum * 100
当前的情况是,它会正确地创建类,但随后它会将其添加到所有td元素中。
示例:页面加载3行表中的每个"description"td元素(表中的第二列)后,将有一个类:描述_0描述_1描述_2
我怎么能得到它,所以第一行的td有类(和只有类):Description_0、unit_cost_0等
第二行的td有类:Description_1, unit_cost_1等
我已经为此工作了2天,找不到一个有效的解决方案。什么好主意吗?
学习下面的例子,根据需要进行调整并应用。
$('tr:first td').addClass('amount');
$('tr:last td').addClass('discount');
$(function() {
$('.amount').attr('class', function(i) {
return 'amount' + (i + 1);
});
$('.discount').attr('class', function(i) {
return 'discount' + (i + 1);
});
});
.amount1,
.amount2,
.amount3,
.discount1,
.discount2,
.discount3 {
font-size: 24px;
font-weight: bold;
font-family: calibri;
}
.amount1 {
color: maroon;
font-size: 24px;
font-weight: bold;
}
.amount2 {
color: green;
}
.amount3 {
color: Orange;
}
.discount1 {
color: black;
}
.discount2 {
color: gold;
font-size: 24px;
font-weight: bold;
}
.discount3 {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="200" border="1">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>5</td>
</tr>
</table>
再次:这只是一个例子,根据需要学习和调整;
相关文章:
- 向Graph中的所有d3.js数据点添加唯一链接
- 向不同的数组元素添加唯一的 css
- 向数组添加唯一元素
- 如何动态地向单选按钮添加唯一 ID
- 如何使用 JavaScript 为元素添加唯一 ID
- 向类中的所有元素添加唯一的侦听器
- MongoDB/Meteor:为每个数组元素添加唯一的ID
- 为标记添加唯一的信息窗口,并更新数据库以存储信息(谷歌地图api)
- 如何为onclick事件添加唯一的id
- Javascript/jQuery:确保在下拉菜单中添加唯一选项
- jQuery UI选项卡示例-打开新选项卡=为每个选项卡添加唯一的脚本
- 自动为PDF下载添加唯一编号
- 仅向数组中添加唯一对象
- 在JavaScript中只向数组添加唯一对象
- 在JQuery中克隆并为每个添加唯一的id
- 如何在循环期间添加onclick事件时添加唯一参数
- 为每行中的每个td添加唯一的类
- 为克隆的表单元素添加唯一名称
- 如何为动态添加的控件添加唯一的id (Asp.net MVC)
- 为防止ie缓存,在Jquery函数url后添加唯一值