使用jquery设置的hiddenfield值返回[object object]

时间:2016-10-20 05:46:25

标签: jquery asp.net-mvc

这是asp.net mvc razor视图设计,用于计算特定行表的数量*价格,它的工作完美,但问题是计算值不存储在隐藏字段中。screen shot of output

@<table id="tbl" class="table table-striped table-hover">
    <thead>
        <tr>
            <th>Product</th>
            <th>Quantity</th>
            <th>Price</th>
            <th>Total</th>
        </tr>
    </thead>
    <tbody>
        @for (var i = 0; i < Model.ListProducts.Count(); i++)
        {
            var pro = Model.ListProducts[i];
            <tr>
                <td>@pro.ShortName @pro.SizeDetails.Size</td>

                <td>@Html.HiddenFor(m => m.ListProducts[i].SizeDetails.SID)
                    @Html.HiddenFor(m => m.ListProducts[i].PID)
                    &nbsp;&nbsp;
                @Html.TextBoxFor(m => m.ListProducts[i].Qty, new { @class = "qty form-control", @style = "width:100px;display:inline;" })</td>
                <td>&nbsp;&nbsp;
                    <i class="fa fa-inr"></i>&nbsp;
                    <label class="">
                        @if (@pro.SizeDetails.DistPrice != null && @pro.SizeDetails.DistPrice != 0)
                        {
                            @pro.SizeDetails.DistPrice
                        }
                        else
                        {
                            @pro.SizeDetails.MRP
                        }
                    </label>
                    <input  type="hidden" value="@if (@pro.SizeDetails.DistPrice != null && @pro.SizeDetails.DistPrice != 0)
                                                       {@pro.SizeDetails.DistPrice}
                                                       else
                                                       {@pro.SizeDetails.MRP}" class="price"/>
                    // @Html.TextBoxFor(m => m.ListProducts[i].finalPrice, new { @id="txtP"})
                </td>
                <td class="total">

                </td>                                
                @Html.HiddenFor(m => m.ListProducts[i].Price, new { @class = "total" ,@id="hdnTot"})
            </tr>

        }

    </tbody>
</table>

脚本

//$('body').on('blur', '.qty, .price', function(e) {
$('.qty, .price').on('blur', function (e) {
    debugger;
    // Get Parent ROW 
    var row = $(this).closest('tr');
    var qty = $('.qty', row),
        pr = $('.price', row),
        tot = $('.total', row);
    $('#hdnTot').val(tot);
    quantity = parseFloat(qty.val());
    price = parseFloat(pr.val());

    if (!isNaN(quantity) && !isNaN(price)) {
        tot.text((quantity * price).toFixed(2));
    }
});

1 个答案:

答案 0 :(得分:0)

使用tot.val((quantity * price).toFixed(2));代替tot.text((quantity * price).toFixed(2));来设置隐藏字段的值。