[英]How do I get these JavaScript buttons to work?
I'm working on a JavaScript chatbot library.我正在开发一个 JavaScript 聊天机器人库。 Certain commands change the buttons available (for example when you initiate the "test command" these options become available: yes, no, & cancel)
某些命令会更改可用按钮(例如,当您启动“测试命令”时,这些选项变得可用:是、否和取消)
When you hit cancel it puts the original buttons back like I intended, but they aren't clickable?当您点击取消时,它会按照我的意图将原始按钮放回原处,但它们不可点击? How can I make them clickable again?
我怎样才能让它们再次可点击?
var commands = [
"test command",
"clear messages"
];
function message(status, message) {
if (status == "sent") {
$(".messages").append("<div class='message sent'><p>" + message + "</p></div>");
} else {
$(".messages").append("<div class='message from'><p>" + message + "</p></div > ");
}
$(".messages").animate({
scrollTop: $('.messages').prop("scrollHeight")
}, 500);
}
function buttons(commands) {
$(".buttons").html("");
$.each(commands, function(index, value) {
var id = value.split(" ").join("-");
$(".buttons").append('<div class="button" id ="command_' + id + '">' + value + '</div>');
});
}
buttons(commands);
$("#command_test-command").click(function() {
message("sent", "test command");
var parameters = [
"yes",
"no",
"cancel"
];
buttons(parameters);
$("#command_yes").click(function() {
message("sent", "yes");
message("from", "you said yes");
});
$("#command_no").click(function() {
message("sent", "no");
message("from", "you said no");
});
$("#command_cancel").click(function() {
message("sent", "cancel");
message("from", "you said cancel");
buttons(commands);
});
});
$("#command_clear-messages").click(function() {
$(".messages").html("");
});
I wouldn't recommend setting html with js but this should trigger the click我不建议用 js 设置 html 但这应该会触发点击
$(".buttons").on('click','#command_test-command',function() {
// test-command
});
$(".buttons").on('click','#command_clear-messages',function() {
// clear messages
});
For your example对于你的例子
var commands = [
"test command",
"clear messages"
];
function message(status, message) {
if (status == "sent") {
$(".messages").append("<div class='message sent'><p>" + message + "</p></div>");
} else {
$(".messages").append("<div class='message from'><p>" + message + "</p></div > ");
}
$(".messages").animate({
scrollTop: $('.messages').prop("scrollHeight")
}, 500);
}
function buttons(commands) {
$(".buttons").html("");
$.each(commands, function(index, value) {
var id = value.split(" ").join("-");
$(".buttons").append('<div class="button" id ="command_' + id + '">' + value + '</div>');
});
}
$(".buttons").on('click','#command_test-command',function() {
message("sent", "test command");
var parameters = [
"yes",
"no",
"cancel"
];
buttons(parameters);
}).on('click','#command_clear-messages',function() {
$(".messages").html("");
}).on('click','#command_yes',function() {
message("sent", "yes");
message("from", "you said yes");
}).on('click','#command_no',function() {
message("sent", "no");
message("from", "you said no");
}).on('click','#command_cancel',function() {
message("sent", "cancel");
message("from", "you said cancel");
buttons(commands);
});
buttons(commands);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.