如何使用Jquery将数字与“Kg”连接起来。我想用单位“Kg”连接TotalWeight字段

时间:2013-07-26 17:40:15

标签: javascript

**HTML Snippet**

<table id="mytable">
@foreach (var item in Model) {
<tr class="item">
    <td>
        @Html.DisplayFor(modelItem => item.OrderId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.CustomerId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.OrderDate)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.OrderQty)
    </td>
    <td class="Weight">
        @Html.DisplayFor(ModelItem => item.TotalWeight, new { id="TotalWeight"})
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.LimeType.LimeTypeName)
    </td>
    </tr>
    </table>
   <script type="text/javascript">
    $(document).ready(function () {
        $("tr.item").each(function () {
            var weight = $(this).find(td[5].val());
            var unit = "Kg";
            var value = weight + unit;
            $("#TotalWeight").val(value);
        });
          });
</script>
}
 html as in markup
 <tbody>
 <tr>
 <tr class="item">
 <td> 1 </td>
 <td> C001 </td>
 <td> 7/26/2013 12:00:00 AM </td>
 <td> 100.00 </td>
 <td class="Weight"> 5000.00 </td>
 <td> Hydrated Lime Powder </td>
 <td> Order Pending </td>
 <td>
 </tr>
 </tbody>

我想将Kg添加到TotalWeight值。我的代码不起作用。它显示了价值 Quantity1为undefined。如何在jQuery中访问表? 我忘了定义重量,但代码仍无效,我发布了html标记

2 个答案:

答案 0 :(得分:1)

$(document).ready(function () {
    $("tr.item").each(function () {
        var weight = $(this).find('td.Weight').text(); // you left out the quotes and the closing paran
        var unit = "Kg";
        $("#TotalWeight").text(weight + unit);
    });
});

jsFiddle DEMO

答案 1 :(得分:0)

你应该实际上计算Scala中的总和(它看起来你正在使用Scala)。此外,您的循环会在每个模型后关闭<table>标记,并且您的<tr>标记永远不会关闭。您还有一个ModelItem,可能与其他地方一样modelItem