【问题标题】:For loop not iterating over items in arrayFor循环不迭代数组中的项目
【发布时间】:2016-03-12 11:15:05
【问题描述】:

我在哪里

我正在寻找与按钮类相关的数组中的项目及其对应的值,以用于无声拍卖的表单。

当一个人出价时,他们将看到最新的出价,然后单击六个预定义值的按钮之一$10, 25, 50, 100, 250, 500 将采用该值,将其添加到.current__amount 然后更改html() 以显示.new__amount

问题

现在,单击六个按钮之一不会将值添加到 .new__amount 它只是保留为占位符文本 "tk-amount" 所以,我想知道这是否是范围问题或者我的 for 循环构造是否关闭?

scripts.js

/*-------------------------------------
STEP ONE: PLACE BID
--------------------------------------*/

// Bid Options
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 }
]

$(".button__form").on('click', function(){
    var btnSelected = $(this).hasClass("is-selected");
    var sectionOneCompleted = $(".check--one").hasClass("is-completed");

    if (btnSelected) {
        $(this).removeClass("is-selected");
        $(".check--one").css("color", "#ccc");
    } else {
        $(".button__form").removeClass("is-selected");
        $(this).addClass("is-selected");
        $(".check--one").css("color", "#ffdc00");
    }
});

    /*-------------------------------------
    API: SHEETSU
    --------------------------------------*/

    $.ajax({
        url: "https://sheetsu.com/apis/4a8eceba",
        method: "GET",
        dataType: "json"
    }).then(function(spreadsheet) {


        // Print current bid
        var currentBid = parseInt(spreadsheet.result.pop().Bids);
        $(".current__amount").html(currentBid);

        // Any of the form buttons
        var $btnForm = $(".button__form");

        for(i = 0; i < buttons.length ; i++){
            if ($btnForm.hasClass(buttons[i].class)){
                var newBid = $(".new__amount").html("$" + (currentBid + buttons[i].value));
                console.log(newBid);
            }
        }
    });

我也试过用这个代替 for 循环,但收效甚微。

        buttons.forEach(function(button) {
            if ($btnForm.hasClass(button.class)){
              var newBid = $(".new__amount").html("$" + (currentBid + button.value));
              console.log(newBid);
            }

index.html

<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 -->

小提琴:http://jsfiddle.net/bqqLxvb9/

【问题讨论】:

  • 首先确定代码是否达到了循环中的那个条件。在里面贴上console.log 什么的。永远不要假设一行代码正在运行。
  • 我的理解是不是,我有 console.log(newBid) 但控制台中没有出现任何内容,所以我对此有点困惑。
  • 您确定没有收到控制台错误吗? class 是保留字,但我注意到您将其声明为对象中的文字属性名称。使用{'class':,而不是{class - 调用它时类似:['class'] 而不是狗语法.class
  • 为避免出现问题,请避免使用class,因为这是一个保留字。 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 考虑使用strict mode,这会警告您使用class 以及缺少的变量声明。

标签: javascript jquery arrays for-loop


【解决方案1】:

您的所有按钮都没有您在 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

<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>

【讨论】:

  • 刚刚解决了这个问题。不过还是有问题
猜你喜欢
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
相关资源
最近更新 更多