我的桌子在http://jsfiddle.net/wqk7Lauz/
我需要做的是将td:nth-of-type(3)中的所有值乘以3.8的页面加载量。
HTML
$('td:nth-of-type(3)').text(parseFloat($('td:nth-of-type(3)').text()) * 3.8)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered mydatatable table-hover display responsive nowrap">
<tbody>
<tr>
</tr>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço por 1000</th>
<th>Quantidade mínima</th>
<th>Quantidade máxima</th>
<th>Descrição</th>
</tr>
<tr>
<td>9</td>
<td>Product </td>
<td>
R$1.86
</td>
<td>100</td>
<td>10000</td>
<td>S1 </td>
</tr>
<tr>
<td>10</td>
<td>Product </td>
<td>
R$3.98
</td>
<td>1000</td>
<td>100000</td>
<td>S1</td>
</tr>
</tbody>
</table>
答案 0 :(得分:1)
您非常接近,您只需要使用text
的回调版本,因此您只需要处理每个单独元素的文本,并在开头删除R$
:>
$('td:nth-of-type(3)').text(function() {
return parseFloat($(this).text().replace("R$", "")) * 3.8;
});
示例:
$('td:nth-of-type(3)').text(function() {
return parseFloat($(this).text().replace("R$", "")) * 3.8;
});
<table class="table table-bordered mydatatable table-hover display responsive nowrap">
<tbody>
<tr>
</tr>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço por 1000</th>
<th>Quantidade mínima</th>
<th>Quantidade máxima</th>
<th>Descrição</th>
</tr>
<tr>
<td>9</td>
<td>Product </td>
<td>
R$1.86
</td>
<td>100</td>
<td>10000</td>
<td>S1 </td>
</tr>
<tr>
<td>10</td>
<td>Product </td>
<td>
R$3.98
</td>
<td>1000</td>
<td>100000</td>
<td>S1</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
如果您想重新添加R$
,并且可能将结果限制在特定的位数,则可以使用字符串串联和toFixed
来做到这一点:
$('td:nth-of-type(3)').text(function() {
return "R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2);
});
示例:
$('td:nth-of-type(3)').text(function() {
return "R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2);
});
<table class="table table-bordered mydatatable table-hover display responsive nowrap">
<tbody>
<tr>
</tr>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço por 1000</th>
<th>Quantidade mínima</th>
<th>Quantidade máxima</th>
<th>Descrição</th>
</tr>
<tr>
<td>9</td>
<td>Product </td>
<td>
R$1.86
</td>
<td>100</td>
<td>10000</td>
<td>S1 </td>
</tr>
<tr>
<td>10</td>
<td>Product </td>
<td>
R$3.98
</td>
<td>1000</td>
<td>100000</td>
<td>S1</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
答案 1 :(得分:0)
您好,以此来更改脚本功能
None
首先,我通过循环获得每三个td值的值
$(".table tr td:nth-child(3)").each(function(){ //Loop
//alert($(this).text())
var thrdtd = $(this).text(); //Getting value of td
onlyno = thrdtd.replace('R$', ''); // Removing R$
//akert(thrdtd);
$(this).text('R$ '+ parseFloat(onlyno * 3.8).toFixed(2)) // Adding R$ and also multiplying at the same time and update that result to td again
});
然后在相乘之前从字符中删除字符,然后在相乘之后我更新相同的td值
答案 2 :(得分:-1)
您可以使用以下代码:
$('td:nth-of-type(3)').text("R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2));