【问题标题】:Value Calculation ASP.net MVC价值计算 ASP.net MVC
【发布时间】:2021-09-18 14:35:32
【问题描述】:

我想通过将 UnitAmount 和用户输入的数量相乘来计算总金额。 然后我需要将该总金额传递给总金额标签字段。到目前为止,这是我的代码,

 <div class="col-md-3 col-sm-6">
   <div class="form-group">
       Unit Amount
      <div class="col-md-10">
       @Html.EditorFor(model => model.Unit_Amount, new { htmlAttributes = new { @class = "form-control" } })
       @Html.ValidationMessageFor(model => model.Unit_Amount, "", new { @class = "text-danger" })
        </div>
   </div>
</div>
<div class="col-md-3 col-sm-6">
   <div class="form-group">
      Requesting Qty
        <div class="col-md-10">
         @Html.EditorFor(model => model.Qty, new { htmlAttributes = new { @class = "form-control" } })
         @Html.ValidationMessageFor(model => model.Qty, "", new { @class = "text-danger" })
        </div>
      </div>
   </div>
<div class="col-md-3 col-sm-6">
     <div class="form-group">
        Total Amount
          <div class="col-md-10">
           @Html.LabelFor(model => model, new { htmlAttributes = new { @class = "form-control" } })
           @Html.ValidationMessageFor(model => model.Qty, "", new { @class = "text-danger" })
          </div>
      </div>
</div>

我也得到了 jquery,但我认为我做错了。

<script>
    $(document).ready(function () {
        $('.Qty').on('change', function (evt) {
            var $row = $(this).parents('tr');
            var rate = Number($row.find('.Unit_Amount').text());
            var qty = Number($(this).val());
            var total = rate * qty;
            $row.find('.Total').text(total);
        });
    });
</script>

你能帮我解决这个问题吗?谢谢。

【问题讨论】:

    标签: jquery asp.net asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    为其中三个项目指定一个唯一的类名。

    像这样:

    @Html.LabelFor(model => model, new { htmlAttributes = new { @class = "form-control totalLabel" } })
    @Html.EditorFor(model => model.Qty, new { htmlAttributes = new { @class = "form-control qtyPrice" } })
    @Html.EditorFor(model => model.Unit_Amount, new { htmlAttributes = new { @class = "form-control unitAmount" } })
    

    在你的脚本中:

    <script>
    $(document).ready(function () {
        $('.Qty').on('change', function () {
            var qtyamount = $('.qtyPrice').val();
            var unitamount = $('.unitAmount').val();
            var total = qtyamount*unitamount;
    
            $(".totalLabel").text(total);
        });
    });
    

    我认为这应该可行。

    编辑

    工作示例:JsFiddle

    【讨论】:

    • 感谢您的解决方案。我试过这个。您的代码似乎可以正常工作,但是当我申请时,什么都没有出现。你能重新检查一下,让我知道我哪里做错了吗? ** `
      总金额
      `**
    • @smcdevelpments 试试这个:$(document).ready(function () { $('.form-control qtyPrice').on('change', function () { var qtyamount = $('.form-control qtyPrice').val(); var unitamount = $('.form-control qtyPrice').val(); var total = qtyamount*unitamount; $(".form-control qtyPrice").text(total); }); });
    • 不 :( 。也试过了。我怀疑我使用的输出标签是这样的。这会是个问题吗??&lt;label id="totalLabel" class="form-control totalLabel"&gt;&lt;/label&gt;
    • 如果你给它分配了一个id,你应该把'#' insted of '。 '
    • 我也试过了,。但不显示总值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    相关资源
    最近更新 更多