javascript - 需要根据开关乘数值
问题描述
在我的 Web 应用程序中,我创建了这个视图。这里它有列项目描述、数量,我再次从模型中获取一些数据并使用模型计数生成其他列。
例如,如果列表中有 2 条记录,则设计创建为
Item Description, Qty, Sup 01, Sup 02
然后我将值从模型加载到该列。还有一个开关按钮来检查线路是否被批准。脚本中有 Ajax 代码可以将该 ID 和开关值传递给控件。
此外,我想在末尾添加另一列作为“总值”。用户将值切换为 true 我想获得该 sup 值并与列相乘,并且需要在该列上显示金额。
这是我尝试过的。但它不起作用
<div class="card-body">
<div class="row">
<div class="col-md-3 col-sm-3">
<strong>Item Description</strong>
</div>
<div class="col-md-1 col-sm-1">
<strong>Quantity</strong>
</div>
@foreach (var item in Model.ToList().First().PurchasingDetailsSup) //new columns will be created with the supplier count that user added earlier
{
<div class="col-md-2 col-sm-2">
<strong> @Suppliers.Find(x => x.Value == item.Supp_Id.ToString()).Text</strong>
</div>
}
</div>
@foreach (Asp_PASMVC.Models.PurchasingDetails item in Model)
{
<div class="row">
<div class="col-md-3">
@item.Item_Description
</div>
<div class="col-md-1">
@item.Qty
</div>
@for (int i = 0; i < item.PurchasingDetailsSup.Count; i++)
{
<div class="col-md-2">
@item.PurchasingDetailsSup[i].Unit_Amount
<input type="checkbox" value="@item.PurchasingDetailsSup[i].IsApproved" class="bootstrap-switch bwitch" data-style="ios" data-id="@item.PurchasingDetailsSup[i].Id" data-onstyle="success" data-offstyle="danger" />
</div>
}
</div>
}
</div>
<script>
$(".bootstrap-switch").change(function () {
var Checked = this.checked;
var Id = $(this).data('id');
var Qty = document.getElementById('Qty').value;
var Amount = document.getElementById('Amount').value;
IsApproved(Id, Checked);
calculate(Qty, Amount);
});
function IsApproved(id, chkValue) {
//alert(chkValue);
$.ajax({
url: '../UpdateApproveStatus',
type: 'POST',
dataType: 'json',
cache: false,
async: false,
data: { Id: id, IsChecked: chkValue },
success: function (data) {
if (data.Success != true) {
console.log('Error In IsApprove');
}
}
});
function calculate(qty,amount) {
var myResult = qty * amount;
alert(myResult)
}
}
</script>
解决方案
您必须将计算函数移出 IsApproved 函数
$(".bootstrap-switch").change(function () {
var Checked = this.checked;
var Id = $(this).data('id');
var Qty = document.getElementById('Qty').value;
var Amount = document.getElementById('Amount').value;
IsApproved(Id, Checked);
calculate(Qty, Amount);
});
function IsApproved(id, chkValue) {
//alert(chkValue);
$.ajax({
url: '../UpdateApproveStatus',
type: 'POST',
dataType: 'json',
cache: false,
async: false,
data: { Id: id, IsChecked: chkValue },
success: function (data) {
if (data.Success != true) {
console.log('Error In IsApprove');
}
}
});
}
function calculate(qty, amount) {
var myResult = qty * amount;
alert(myResult)
}
推荐阅读
- mysql - nodejs 服务器 app.post 问题 XML 解析错误:语法错误位置:http://localhost:3000/get_messages 第 1 行第 1 列:
- c# - WPF中切换用户控件只显示ViewModel的名称
- excel - 组合框列出路径中的最新 2 个文件夹
- r - 是否可以以水平和垂直显示总和的方式来 table() 两列?
- php - 使用 FrontController 将元素添加到挂钩
- c++ - 这两个连接字符串的 C++ 语句有什么区别?
- algorithm - 基于与某个单元格的接近度循环遍历 NxN 个网格单元格
- marklogic - 恢复是否强制为目标数据库中的新自定义字典索引重新索引数据库?
- html - 更改相对于上一个类的字体大小
- ssl - libucrl 未能检查服务器证书