Javascript For循环不迭代数组中的项 我在哪里
我希望在一个数组中循环项目,该数组与按钮类及其对应的静默拍卖表单值相关 当一个人出价时,他们将看到最新的出价,然后单击六个预定义值按钮之一Javascript For循环不迭代数组中的项 我在哪里,javascript,jquery,arrays,for-loop,Javascript,Jquery,Arrays,For Loop,我希望在一个数组中循环项目,该数组与按钮类及其对应的静默拍卖表单值相关 当一个人出价时,他们将看到最新的出价,然后单击六个预定义值按钮之一$10、25、50、100、250、500将获取该值,将其添加到。当前金额,然后更改html()以显示。新金额 问题 现在,单击六个按钮中的一个按钮不会将值添加到。新的\uuu金额它只是作为占位符文本“tk金额”保留。因此,我想知道这是否是一个范围问题,还是我的for循环构造已关闭 scripts.js index.html $10 $25 $50 $100
$10、25、50、100、250、500
将获取该值,将其添加到。当前金额
,然后更改html()
以显示。新金额
问题
现在,单击六个按钮中的一个按钮不会将值添加到。新的\uuu金额
它只是作为占位符文本“tk金额”保留。因此,我想知道这是否是一个范围问题,还是我的for循环构造已关闭
scripts.js
index.html
$10
$25
$50
$100
$250
$500
当前的投标是
tk金额
您的出价将
tk金额
Fiddle:您的按钮中没有一个包含您在javascript中定义的类: JavaScript
var buttons = [
{ class: "buttonOne", value: 10 },
{ class: "buttonTwo", value: 25 },
{ class: "buttonThree", value: 50 },
{ class: "buttonFour", value: 100 },
{ class: "buttonFive", value: 250 },
{ class: "buttonSix", value: 500 }
]
HTML
$10
$25
$50
$100
$250
$500
首先,我要确定代码是否达到了循环中的条件。把console.log
放在那里什么的。永远不要假设一行代码正在运行。我的理解是,它不是,我有console.log(newBid),但在控制台中没有显示任何内容,所以我对此有点困惑。你确定没有收到控制台错误吗class
是一个保留字,但我注意到您在对象中将其声明为文字属性名。使用{'class':
,而不是{class
-在调用它时类似地:['class']<代码> >而不是狗语法>代码>类< /代码>以避免沿路出现问题,避免使用<代码>类< /代码>,因为这是一个保留字。考虑使用,这将警告您使用<代码>类< /代码>以及您丢失的变量声明。
buttons.forEach(function(button) {
if ($btnForm.hasClass(button.class)){
var newBid = $(".new__amount").html("$" + (currentBid + button.value));
console.log(newBid);
}
<div class="buttons">
<button class="button__form button__one">$10</button>
<button class="button__form button__two">$25</button>
<button class="button__form button__three">$50</button>
<button class="button__form button__four">$100</button>
<button class="button__form button__five">$250</button>
<button class="button__form button__six">$500</button>
</div><!-- /.buttons -->
<div class="bids__amounts">
<div class="bids__amount bids__current">
<p class="bids__note">The current bid is</p>
<h4 class="current__amount">tk-amount</h4>
</div>
<div class="bids__amount bids__new">
<p class="bids__note">Your bid will be</p>
<h4 class="new__amount">tk-amount</h4>
</div><!-- /.bids__amount -->
</div><!-- /.bids__amounts -->
var buttons = [
{ class: "buttonOne", value: 10 },
{ class: "buttonTwo", value: 25 },
{ class: "buttonThree", value: 50 },
{ class: "buttonFour", value: 100 },
{ class: "buttonFive", value: 250 },
{ class: "buttonSix", value: 500 }
]
<div class="buttons">
<button class="button__form button__one">$10</button>
<button class="button__form button__two">$25</button>
<button class="button__form button__three">$50</button>
<button class="button__form button__four">$100</button>
<button class="button__form button__five">$250</button>
<button class="button__form button__six">$500</button>
</div>