【问题标题】:Getting value of last clicked item from array从数组中获取最后点击项目的值
【发布时间】:2016-03-13 23:19:46
【问题描述】:

更新 #2:更新脚本。

目标

a) 用户选择一个按钮。最后点击按钮的值 + .current__amount = new__amount

b) 没有累计。再次单击同一个按钮应取消选择它,然后从.new__amount 中减去该值,然后使用.html() 更改占位符文本

问题

好吧,现在无论出于何种原因,单击按钮都不会在.new__amount 中添加或删除其值。

我有 console.log(buttons[i].value)console.log(buttons[i].class) 并且可以看到 for 循环正在打印这六个按钮的类和值,它们代表无声拍卖中的出价 $10, 25, 50 并已存储在一个数组中像我想要的那样叫var buttons = []

scripts.js(更新)

差不多了。只需要做到一次只能选择一个按钮。

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

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

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

    $('.button__form').on('click', function() {
        var value = $(this).val();
        if($(this).hasClass('is-selected')) {
            $(this).removeClass('is-selected');
            $(".check--one").css("color", "#ccc");
            currentBid = parseInt(currentBid) - parseInt(value);
        }
        else {
            $(this).addClass('is-selected');
            $(".check--one").css("color", "#ffdc00");
            currentBid = parseInt(currentBid) + parseInt(value);
        }
        $('.total__amount').html("$" + currentBid);
    });
});

【问题讨论】:

  • 您可以通过这种方式获取数组中的最后一项:myArray[myArray.length - 1]... 不过我不完全确定您在问什么。
  • 你只是问怎么办new = current + last clicked
  • @indubitablee 是的。这样我就可以将页面上占位符的文本更改为 new 的值。
  • 抱歉,在所有选择/取消选择的情况下,很难按照您对代码的操作进行操作。您最终想要的功能是什么?是总结所有选定的按钮值吗? (这样通过单击一次,您选择了该值并将该值添加到总计中,然后再次单击,您取消选择并从总计中减去该值)或者您正在保持运行总计? (因为你按了值 5 按钮 3 次,总共得到 15)?
  • @Jacques 我试图提供一些更清晰的细节,希望它更有意义。基本上,我希望打印 .new__amount 的值,这是 .currentBid + theValueoftheLastButtonClicked 的组合值

标签: javascript jquery arrays if-statement for-loop


【解决方案1】:

这并不是特定于您的问题结构,但它应该让您了解它是如何工作的。

$(document).ready(function() {
  var totalAmount = 0;
  $('.button__form').on('click', function() {
    var value = $(this).val();
    if($(this).hasClass('selected')) {
      $(this).removeClass('selected');
      totalAmount = parseInt(totalAmount) - parseInt(value);
    }
    else {
      $(this).addClass('selected');
      totalAmount = parseInt(totalAmount) + parseInt(value);
    }    
    $('.total').html(totalAmount);
  });
});
.selected {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="button__form" value=10>10</button>
<button class="button__form" value=25>25</button>
<button class="button__form" value=50>50</button>
<button class="button__form" value=100>100</button>
<button class="button__form" value=250>250</button>
<button class="button__form" value=500>500</button>
<br/><br/>
<div class="total"><div>

编辑一次可选择一个按钮:

$(document).ready(function() {
  var baseAmount = 0;
  var totalAmount = baseAmount;
  $('.button__form').on('click', function() {
    var value = $(this).val();
    if($(this).hasClass('selected')) {
      $(this).removeClass('selected');
      totalAmount = parseInt(totalAmount) - parseInt(value);
    }
    else {
      $('.button__form').removeClass('selected'); // remove selected css from all the other buttons
      $(this).addClass('selected');
      totalAmount = baseAmount; // reset the totalAmount to the original base amount
      totalAmount = parseInt(totalAmount) + parseInt(value);
    }    
    $('.total').html(totalAmount);
  });
});
.selected {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="button__form" value=10>10</button>
<button class="button__form" value=25>25</button>
<button class="button__form" value=50>50</button>
<button class="button__form" value=100>100</button>
<button class="button__form" value=250>250</button>
<button class="button__form" value=500>500</button>
<br/><br/>
<div class="total"><div>

【讨论】:

  • 它可以工作,但是,当我只想加减按钮的值一次时,它会在按钮单击时多次加减。有什么建议吗?
  • 所以一旦您选择和取消选择同一个按钮,就再也不能添加它了吗? (如果你每次点击所有按钮 2 次,你最终会得到 0 的总值?)
  • 我已经根据我的需要稍微改变了你的 sn-p,但基本上我唯一需要解决的是我可以一次选择多个按钮,而我没有想要做。我已在原始帖子中更新了我的 sn-p。
  • 所以你说在任何给定时间只能选择 1 个按钮。如果我选择 10 那么 50, 10 将自动取消选择并且总值将显示 50(不是 60)?
  • 没错。一次选择一个按钮。因此,如果您的 current value = 0,选择 10 美元的按钮应该使您的总价值为 current amount + button's value,即 10 美元。但是,如果您随后按下 50 美元按钮,则应取消选择前一个按钮,并且该按钮的值会从总数中减去,从而为您留下新的 50 美元。
猜你喜欢
  • 2021-04-05
  • 2011-01-17
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
相关资源
最近更新 更多