基于剃须刀循环 jquery 循环
Loop in jquery based on razor loop
我的 razor 视图 (MVC4) 中有一个 for each 循环
@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])
});
});
但这只是考虑最后一个foreach raw。我如何在每个循环调用中调用这个 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]);
})
我的 razor 视图 (MVC4) 中有一个 for each 循环
@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])
});
});
但这只是考虑最后一个foreach raw。我如何在每个循环调用中调用这个 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]);
})