【发布时间】:2017-09-26 07:43:06
【问题描述】:
我想创建一个点击事件。
但是controlCount()中console.log的值不一样。
function Spinbox() {
this.MIN_COUNT = 180;
this.MAX_COUNT = 220;
this.$inputBox = $(`<input type="text"/>`);
this.$increaseButton = $(`<button type="button">+</button>`);
this.$decreaseButton = $(`<button type="button">-</button>`);
}
Spinbox.prototype.controlCount = function() {
console.log(this.$inputBox.val());
// not working. because this = <button type="button">+</button>
}
Spinbox.prototype.create = function() {
this.$increaseButton.click(this.controlCount);
$("#wrap").append(this.$inputBox);
$("#wrap").append(this.$increaseButton);
$("#wrap").append(this.$decreaseButton);
}
var spinbox1 = new Spinbox();
spinbox1.create();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrap">
</div>
【问题讨论】:
-
你为什么要获取按钮的
val?你想要按钮的text吗?this.$increaseButton.text()?那只会给你+符号。 -
对不起,我的错误。我修改了我的问题
标签: javascript jquery oop