jquery中基于剃刀循环的循环

Loop in jquery based on razor loop

本文关键字:循环 剃刀 于剃刀 jquery      更新时间:2023-09-26

在我的剃刀视图(MVC4)中,每个循环都有一个

 @foreach (var fe in ViewBag.FeatureProducts)
  {
    <div class="product-details" style="display:none;">@Html.Raw(fe.Details)</div>
    <div class="product-qty-dt">   </div>
  }      

在每个循环中,我必须提取@html行内容并显示在div"product qty dt"中。

为此,我编写了以下jquery,

$(document).ready(function () {        
       var data = $('.product-details p').map(function () {
           return $(this).text();
       }).get();           
       //product availability
       var availability = data[2].split(",");        
       $.each(availability, function (i) {
           $('.product-qty-dt').append( availability[i])
       });
   });

但这只是被认为是最后一个未经处理的前臂。如何在每次循环调用中调用这个jquery。

例如,在第一个循环中,

  <div class="product-details" style="display:none;"><p>Lorem, Ispum</p><p>doler emit,fghjh</p><p>9gm</p></div>
  <div class="product-qty-dt"> 9gm  </div>

第二环路

 <div class="product-details" style="display:none;"><p>Lorem, Ispum</p><p>doler emit,fghjh</p><p>5gm</p></div>
 <div class="product-qty-dt"> 5gm  </div>

第三环路

 <div class="product-details" style="display:none;"><p>Lorem, Ispum</p><p>doler emit,fghjh</p><p>3gm</p></div>
 <div class="product-qty-dt"> 3gm  </div>

下面将把<div class="product-details">元素中最后一个<p>的文本添加到相应的<div class="product-qty-dt">元素中

$('.product-details').each(function () {
  var t = $(this).children('p').last().text();
  $(this).next('.product-qty-dt').text(t);
}) 

这样就可以了。此外,data数组仍在使用,以防您想访问存储在产品详细信息中的其他数据部分。

$(".product-details").each(function()
    {
        var data = $(this).find('p').map(function () { return $(this).text() }).get();
        $(this).next('.product-qty-dt').text(data[2]);
    })