내 코드가 작동하고 첫 번째 줄이 올바르게 표시되지만 두 번째 줄은 첫 번째 줄과 연결됩니다.

Nasreen
<script type="text/javascript">
    $(document).ready(function () {
        $("tr.item").each(function () {
            var weight = $(this).children('td.Weight').text();
            var unit = "Kg";
            $(".Weight").text(weight + unit);

            });


    });
</script>
 html code...
  @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>
  </tr>

값은 5000Kg, 5000KgKg, 5000KgKgKg 등의 요소와 계속 연결됩니다. 현재 값을 고려할 수 없으며 코드는 모든 행을 고려합니다.

스테판

에서 사용 된 선택기 $(".Weight").text(weight + unit);는 현재 tr요소 내의 요소 뿐만 아니라 "Weight"클래스를 가진 모든 요소 선택합니다 . $(this).children("td.Weight")그래도 사용하여 올바른 요소를 찾을 수 있으므로 해당 요소를 다시 사용하십시오 .

$("tr.item").each(function () {
  var $weight = $(this).children(".weight"),
      weight = $weight.text(),
      unit = "Kg";

  $weight.text(weight + unit);
});

BTW : 더 짧은 선택기를 사용하는 것이 좋습니다.이 경우 .Weight에는 충분하고 클래스 이름과 일치 할 수 있습니다 ( "Weight"를 소문자로 작성).

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관