首页 > 解决方案 > 需要根据开关乘数值

问题描述

在我的 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>

标签: javascriptc#asp.netajax

解决方案


您必须将计算函数移出 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)

}

推荐阅读