for循环不迭代数组中的项

时间:2015-12-07 18:25:53

标签: javascript jquery arrays for-loop

我在

的地方

我希望循环播放与按钮相关的数组中的项目。类及其对应的静态拍卖形式的价值。

当某人出价时,他们会看到最新出价,然后点击六个预定义值$10, 25, 50, 100, 250, 500的其中一个按钮将获取该值,将其添加到.current__amount然后更改{ {1}}显示html()

问题

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

scripts.js中

.new__amount

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

/*-------------------------------------
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);
            }
        }
    });

的index.html

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

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

1 个答案:

答案 0 :(得分:0)

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