javascript - addeventlistener click 仅在第一次单击时起作用

标签 javascript addeventlistener

一直在尝试重新创建旧的掌上飞行员毒品 war 游戏,但无法弄清楚代码的哪一部分搞砸了。单击 + 按钮时尝试将值加 1。这是代码,谢谢!

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;
},

这是所有代码的 jsbin,以防我错过其他内容

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

最佳答案

如果不查看所有代码,很难判断,但我相信以下更改应该有效。

this.acidQuantity: 1,

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

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

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

关于javascript - addeventlistener click 仅在第一次单击时起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39186835/

相关文章:

javascript - MDN 上的代码 : how does this IE specific shim for setTimer work?

javascript - AngularJS 下拉菜单始终处于打开状态

javascript - 如何制作子页面?

闪光灯, ActionScript 3 : how do you give a the EventListener a function With parameters?

javascript - 如何删除元素中的所有监听器?

javascript - 在其他 ID 中显示来自 ID 的文本

javascript - Jquery 的恶意软件脚本

javascript - addEventListener 无法在旧版 Chrome 中正确处理键

javascript - 在 React 中更改 mousedown 和 mousemove 的宽度

JavaScript 将事件附加到动态呈现的元素