Javascript html表中可编辑字段的更改事件仅影响顶行计算字段
下表中有可编辑的字段(列) 我对sellprice或casecost列字段所做的任何更改,都希望GP列字段随着用户类型或值的更改而更新。我的代码如下Javascript html表中可编辑字段的更改事件仅影响顶行计算字段,javascript,jquery,html,razor,Javascript,Jquery,Html,Razor,下表中有可编辑的字段(列) 我对sellprice或casecost列字段所做的任何更改,都希望GP列字段随着用户类型或值的更改而更新。我的代码如下 function calculate() { $('tr #item_q_sellprice').change(function calculate() { //$('.productTable tr').on('change','#item_q_sellprice,#item_q_casecos
function calculate() {
$('tr #item_q_sellprice').change(function calculate() {
//$('.productTable tr').on('change','#item_q_sellprice,#item_q_casecost',function calculate() {
// $("input[type=text]").change(function () {
//fields used for calculations
var newPrice = parseFloat($('tr #item_q_sellprice').val());
var casecost = parseFloat($('tr #item_q_casecost').val());
var casesize = parseFloat($('tr #item_q_casesize').val());
var vatrate = parseFloat($('tr #item_productVat').val());
//formulae
var netprice = newPrice / (1 + vatrate / 100);
var unitcost = casecost / casesize;
var profit = (newPrice / (vatrate / 100 + 1)) - unitcost;
var grossprofit = (profit / netprice) * 100
//alert("change price: " + price);
//Update GP field
$('#item_grossProfit').val(grossprofit.toFixed(1));
});
}
我的行的Html如下所示
@foreach (var item in Model)
{
<tr>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.DisplayFor(modelItem => item.q_description)
</td>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.DisplayFor(modelItem => item.q_barcode)
</td>
<td>
€ @Html.TextBoxFor(modelItem => item.q_sellprice, "{0:0.00}", new { @class = "calc-list" })
</td>
<td>
€ @Html.TextBoxFor(modelItem => item.q_casecost, "{0:0.00}", new { @class = "calc-list" })
</td>
<td>
@Html.TextBoxFor(modelItem => item.grossProfit, new { @class = "calc-list" })
</td>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.DisplayFor(modelItem => item.productDepartment)
</td>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.TextBoxFor(modelItem => item.productVat, "{0:0.0}",new { @class = "calc-list", @readonly = "readonly" })
</td>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.DisplayFor(modelItem => item.q_stocklevel)
</td>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.TextBoxFor(modelItem => item.q_casesize, new { @class = "calc-list", @readonly = "readonly" })
</td>
</tr>
}
如何使这些字段(sellprice/casecost)发生更改/更新的任何行也更新相应的GP列?您使用的
foreach
循环正在生成无效html的重复id
属性,这也是脚本失败的原因。例如,使用$('tr#item_q_sellprice').val()
将只返回具有该id的第一个元素的值
更重要的是,使用foreach
循环意味着您的视图永远不会正确绑定。相反,您需要使用for
循环或EditorTemplate
-有关更多详细信息,请参阅)
为计算中需要的元素指定一个类名,然后使用相对选择器在同一容器(您的
)中查找相关元素
视图应为(使用for
循环)
作为旁注,您应该检查值是否有效(例如,如果文本框中输入的值无效,parseFloat
和Number()
都将返回NAN
)
if (isNaN(newPrice) || isNaN(casecost) {
// its invalid and the calculation would fail
此外,由于grossProfit
是一个计算值,因此不应将其生成为文本框。取而代之的是在
中使用(比如)一个
来显示计算出的值(该值应该在POST方法中在服务器上重新计算,以防止恶意用户更改请求)请同时发布您的HTML。.给您的源文本字段一个公共类,以便您可以一次性选择它们。研究jQuery的树遍历方法,如:parent、closest、next和prev duplicate HTML id无效。改用类。这完全符合我的需要。非常感谢@Stephen
@for(int i = 0; i < Model.Count; i++)
{
<tr>
....
@Html.TextBoxFor(m => m[i].q_sellprice, "{0:0.00}", new { @class = "sell-price calc-list" })
@Html.TextBoxFor(m => m[i].q_casecost, "{0:0.00}", new { @class = "case-cost calc-list" })
....
</tr>
}
$('.sell-price, .case-cost').change(function calculate() {
// Get the containing table row
var row = $(this).closest('tr');
var newPrice = Number(row.find('.sell-price').val());
var casecost = Number(row.find('.case-cost').val());
....
// calculate results
row.find('.gross-profit').val(grossprofit.toFixed(1))
});
if (isNaN(newPrice) || isNaN(casecost) {
// its invalid and the calculation would fail