如何根据是否在ASP.NET MVC和C#中检查复选框在文本框中自动计算值?
我有一个列表视图,列出了按订单#(OrderID
)订购的所有产品。我在每个产品上添加了一个复选框和一个文本框。
我正在尝试将用户打开表单时,当他们从特定订单中列出产品列表时,如果他们检查复选框,它将自动计算一个值,以插入文本框中。
例如,我要添加两列以获取文本框的值(QTYORDERDED
和Qtyavailable
)。因此,假设订单1:Producta的Qtyavailaboug为4,对于该订单,他们订购了2。
我想发生的是如果用户检查复选框,则将“ qtyOrdered” +“ qtyavailable”的值(将是6)进入文本框。如果用户取消选中复选框,则该文本框值将返回到null或空。
我要这样做的原因是因为当我们收到订单时,有时并不是该订单中的所有产品都会回来,因此我希望能够检查我们收到的哪些产品,以便将其添加回库存。然后,当提交表单时,我将使用文本框值在另一个表中更新Qtyavailable
列,但是我只需要求解我遇到的复选框和文本框问题时。
我正在努力使它正常工作。我只适用于第一个产品,但是如果我单击任何其他复选框,则什么都不会发生。另外,当我取消选中复选框时,我无法让它更改为空或空。任何帮助都很棒,我是ASP.NET MVC和C#的新手,并且非常感谢关于我做错什么的任何建议。
到目前为止,这是我的查看页面:
<table class="table">
<tr>
<th>
Order #
</th>
<th>
Product Code
</th>
<th>
Qty Ordered
</th>
<th>
Qty Available
</th>
<th>
Qty Returned
</th>
<th>
Return?
</th>
<th>
New Qty Available
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelitem => item.OrderID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Product_Code)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty_Ordered)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty_Available)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty_Returned)
</td>
<td>
<input type="checkbox" name="return" onclick="newQuantity(this)">
</td>
<td>
<input class="ctl-number" type="number" name="newQty" id="nsq" />
</td>
</tr>
<script>
function newQuantity() {
var returned = [];
$('input[type="checkbox"]').each(function () {
if ($(this).is(':checked')) {
returned.push($(this).val());
var qty = @item.Qty_Ordered.Value + @item.Qty_Available.Value;
$('#nsq').val(qty);
}
})
}
</script>
}
</table>
I have a list view that lists all of the products that were ordered per order# (OrderID
). I added a checkbox and a textbox on each product.
I'm trying to make it to where when the user opens up the form, when they go down the list of products from a particular order, if they check the checkbox it will auto calculate a value to insert into the textbox.
For example, there's two columns I want to add together to get the value for the textbox (QtyOrdered
and QtyAvailable
). So let's say for Order# 1: ProductA has a QtyAvailable of 4 and for that order they ordered 2.
What I want to happen is if the user checks the checkbox, then it would put the value of "QtyOrdered" + "QtyAvailable" (which would be 6) into the textbox. And if the user unchecks the checkbox, then that textbox value would go back to null or empty.
The reason I want to do this is because when we receive an order back, sometimes not all the products from that order come back so I want to be able to check which products we received back so it will add it back to inventory. I'm then going to use the textbox values to update the QtyAvailable
column in another table when the form is submitted, but I just need help with solving the checkbox and textbox issue I'm having.
I am struggling to get it working properly. I got it working for just the first product, but if I click on any additional checkboxes, nothing happens. Also, I can't get it to change back to null or empty when I uncheck the checkbox. Any help would be great, I'm new to ASP.NET MVC and C# and would really appreciate any advice on what I'm doing wrong.
So far here is my view page:
<table class="table">
<tr>
<th>
Order #
</th>
<th>
Product Code
</th>
<th>
Qty Ordered
</th>
<th>
Qty Available
</th>
<th>
Qty Returned
</th>
<th>
Return?
</th>
<th>
New Qty Available
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelitem => item.OrderID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Product_Code)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty_Ordered)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty_Available)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty_Returned)
</td>
<td>
<input type="checkbox" name="return" onclick="newQuantity(this)">
</td>
<td>
<input class="ctl-number" type="number" name="newQty" id="nsq" />
</td>
</tr>
<script>
function newQuantity() {
var returned = [];
$('input[type="checkbox"]').each(function () {
if ($(this).is(':checked')) {
returned.push($(this).val());
var qty = @item.Qty_Ordered.Value + @item.Qty_Available.Value;
$('#nsq').val(qty);
}
})
}
</script>
}
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论