简体   繁体   English

addeventlistener仅在首次点击时起作用

[英]addeventlistener click only working on first click

Been trying to recreate the old palm pilot dope wars game and can't figure out what part of the code is messing this up. 一直在尝试重新创建旧的掌上飞机的“涂料大战”游戏,无法弄清楚代码的哪一部分将其弄乱了。 Trying to add 1 to the value when you click the + button. 当您单击+按钮时尝试将1加到该值。 Here is the code thanks! 这是代码,谢谢!

init: function(){
  this.acid = document.getElementById('acid');
  this.acidQty = document.getElementById('acidQty');
  this.acidAdd = document.getElementById('acidAdd');
},
// bind the events to the button
bindEvents: function(){
  this.acidAdd.addEventListener('click', this.addAcid.bind(this), false);
  // $("#acidAdd").on('click', this.addAcid.bind(this));
},
//Get the value
getQty: function(){
  this.acidQuantity = $('#acidQty').val();
},
//get current value and add 1
addAcid: function(){
  this.getQty();
  this.acidQuantity ++;
  this.updateQuantity();
},
// update the value on the html
updateQuantity: function(){
  this.acidQty.innerText = this.acidQuantity;
},

Here is a jsbin of all the code incase I missed something else 这是所有代码的jsbin,以防我错过了其他东西

http://jsbin.com/wakoqeduwo/1/edit?html http://jsbin.com/wakoqeduwo/1/edit?html

It is difficult to tell without seeing all the code, but I believe the following changes should work. 没有看到所有代码很难说,但我相信以下更改应该起作用。

this.acidQuantity: 1,

bindEvents: function(){
   this.acidAdd.addEventListener.addEventListener('click', this.addAcid);
},

addAcid: function(){
  this.updateQuantity();
  this.acidQuantity ++;
},

updateQuantity: function() {
    $('#acidQty').html(app.acidQuantity); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM