Javascript 倒计时不在 C# Foreach 循环中迭代

我写了一个 foreach 循环代码来遍历产品列表(产品通过 jquery 内容滑块旋转),我想在这个循环中添加一个 javascript 倒计时,但问题是倒计时只出现在这个循环的第一个产品中,如下所示:

http://img4.mukewang.com/6167eeb90001aab705070345.jpg

但是当它遍历其他产品时,它没有出现

http://img3.mukewang.com/6167eec500016f8505160370.jpg

这是我写的代码


<div id="rightBox">

        @foreach (var product in Model)

        {

            <div id="fragment-@product.Id" class="ui-tabs-panel" style="">

                <div style="float: right">

                    <div class="banner"></div>

                    <img class="img-content" src="~/Content/ProductImages/@product.Image" alt="" />

                </div>

                <div class="content">

                    <div class="column">

                        <span class="fa" style="text-decoration: line-through; font-size: 1.9rem">@product.MainPrice.ToString("N0") تومان</span>

                        <div style="margin: 10px 10px 10px 10px;"></div>

                        <span class="fa" style="color: #ef5661; font-size: 1.9rem">@string.Format("{0:N0}", product.DiscountPrice) تومان</span>

                    </div>

                    <div class="columnleft fa">

                        ٪@Math.Floor((1 - (product.DiscountPrice / product.MainPrice)) * 100)&emsp;

                        <span> تخفیف </span>

                    </div>

                    <div class="title">

                        <p>@product.Name</p>

                    </div>

                    <br />

                    <div class="desclist">

                        @Html.Raw(product.Description)

                    </div>

                    <div class="fa" style="bottom: 50px; position: absolute">

                        <hr />

                        <div class="fa" style="font-size: x-large">

                            <span class="fa" id="hrRemaining"></span>:<span id="minRemaining"></span>:<span id="secRemaining"></span>

                        </div>                            

                        <p>زمان باقیمانده تا پایان سفارش</p>

                    </div>

                </div>

            </div>

        }

    </div>

海绵宝宝撒
浏览 172回答 2
2回答

千万里不及你

第一个也是主要的问题是您呈现的 HTML 中有重复的 ID 值。当您尝试使用 getElementById 选择这些元素时,这将导致问题。所以,让我们先解决这个问题:<div id="rightBox">&nbsp; &nbsp; @foreach (var product in Model)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; <div id="fragment-@product.Id" class="ui-tabs-panel" style="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div style="float: right">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="banner"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img class="img-content" src="~/Content/ProductImages/@product.Image" alt="" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="column">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="fa" style="text-decoration: line-through; font-size: 1.9rem">@product.MainPrice.ToString("N0") تومان</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div style="margin: 10px 10px 10px 10px;"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="fa" style="color: #ef5661; font-size: 1.9rem">@string.Format("{0:N0}", product.DiscountPrice) تومان</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="columnleft fa">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ٪@Math.Floor((1 - (product.DiscountPrice / product.MainPrice)) * 100)&emsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span> تخفیف </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="title">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>@product.Name</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="desclist">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.Raw(product.Description)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="fa" style="bottom: 50px; position: absolute">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <hr />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="fa countdown-timer" style="font-size: x-large">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="fa hrRemaining"></span>:<span class="minRemaining"></span>:<span class="secRemaining"></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>زمان باقیمانده تا پایان سفارش</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; }</div>我已经id用类替换了您的属性,我们可以在下面添加的代码中利用这些属性。由于您使用 jQuery 标记了它,因此我重新编写了您的示例以使用 jQuery 而不是普通的 JavaScript:var remSeconds = Math.floor(@timeRemaining);var secondsCounter = Math.floor(remSeconds % 60);var minutesCounter = Math.floor((remSeconds / 60) % 60);var hoursCounter = Math.floor((remSeconds / 3600));function formatNumber(number) {&nbsp; &nbsp; if (number < 10)&nbsp; &nbsp; &nbsp; &nbsp; return '0' + number;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; return '' + number;}function startTimers(){&nbsp; &nbsp; // The variables prefixed with `$` represent jQuery objects. Using $ isn't necessary, but it&nbsp;&nbsp; &nbsp; // makes it a bit more obvious that it's not a "data" variable&nbsp; &nbsp; // This will select all elements with the class 'countdown-timer'&nbsp; &nbsp; var $timers = $('.countdown-timer');&nbsp; &nbsp; // See https://api.jquery.com/each/&nbsp; &nbsp; $timers.each(function(){&nbsp; &nbsp; &nbsp; &nbsp; // `this` is the current `.countdown-timer` being iterated by jQuery.each()&nbsp; &nbsp; &nbsp; &nbsp; var $timer = $(this);&nbsp; &nbsp; &nbsp; &nbsp; var $seconds = $timer.find('.secRemaining');&nbsp; &nbsp; &nbsp; &nbsp; var $minutes = $timer.find('.minRemaining');&nbsp; &nbsp; &nbsp; &nbsp; var $hours = $timer.find('.hrRemaining');&nbsp; &nbsp; &nbsp; &nbsp; $seconds.text(formatNumber((econdsCounter));&nbsp; &nbsp; &nbsp; &nbsp; $minutes.text(formatNumber(minutesCounter));&nbsp; &nbsp; &nbsp; &nbsp; $hours.text(formatNumber(hoursCounter));&nbsp; &nbsp; &nbsp; &nbsp; var _tick = setInterval(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (remSeconds > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (hoursCounter > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (minutesCounter == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minutesCounter = 60;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hoursCounter = hoursCounter - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (secondsCounter == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secondsCounter = 60;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minutesCounter = minutesCounter - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secondsCounter = secondsCounter - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remSeconds = remSeconds - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $seconds.text(formatNumber((econdsCounter));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $minutes.text(formatNumber(minutesCounter));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hours.text(formatNumber(hoursCounter));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearInterval(_tick);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //document.getElementById("tRemaining").innerHTML = "EXPIRED";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }, 1000);&nbsp; &nbsp; }}startTimers();所以,回顾一下变化:我添加了一个名为类countdown-timer的<div>,你用它来包含你的小时,分钟和秒元素的元素。我切换了id属性的class属性,这让我们可以重用我们的“选择器”——标识我们的计时器元素的类我将函数重命名为startTimers,但这只是因为(对我而言)它更准确地描述了函数的用法。在 中startTimers,我使用 jQuery 选择每个计时器元素(由 表示countdown-timer),这将返回一些我可以将其视为数组的内容。然后我使用该$('selector').each()函数遍历定时器数组,设置匹配元素的文本值并启动setInterval循环。

皈依舞

这是因为您使用的是 id。ID 在 HTML 中必须是唯一的。如果这些项目中的每一个都有相同的剩余时间,那么只需将 id 更改为一个类。如果它们是唯一的倒计时,那么您可能应该将它们添加到您的模型中并为每个产品生成一个唯一的 id。那么显然你需要修改你的javascript来跟踪id。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript