我有一个包含多个按钮的类,用户可以从中选择一个数字,但是在点击侦听器触发一次后,它不再被调用。代码笔 ( https://codepen.io/jasonws/pen/qBZeMMB ) 是一个显着简化的版本,它仍然显示问题。任何人都可以看看这个并让我知道我做错了什么吗?谢谢!
class Utilities {
selectedSession = "Second";
constructor () {
const self = this;
this.manage();
$("button").on("click", function(e) {
self.message("Button click: switching to " + this.value + ".");
self.selectedSession = this.value;
self.manage();
});
}
manage() {
const self = this;
const sessions = [ "First", "Second", "Third", "Fourth", "Fifth" ]
var code;
$("#picker").empty();
this.message("Manage: " + this.selectedSession + " is selected.");
sessions.forEach(function(entry) {
code = "<button type='button' value = '" + entry + "'";
if (entry == self.selectedSession) { code += " style='background-color: gray;'"}
code += ">" + entry + "</button>";
$("#picker").append(code);
});
$("#content").text($("#picker")[0].outerHTML);
}
message(text) {
if ($("#messages").text() != "") { $("#messages").append("<br>"); }
$("#messages").append(text);
}
}
test = new Utilities();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="picker"></div>
<textarea id="content" rows="10" cols="50"></textarea>
<div id="messages"></div>
红颜莎娜
相关分类