我为自动回答问题而编写的代码片段正在跳过第一个回复。
我希望它做的另一件事是在发送数组中的最后一条消息后隐藏sendButton
。
我尝试在消息数组中放置一个.hide(),希望它将它用作禁用按钮的最后一个响应,但除了搞乱代码之外,这似乎没有做什么哈哈。
如果有人可以帮助我,我会非常感激!
代码:
$(window).load(function(){
$(document).ready(function() {
$("#typing").hide();
var n = "You:<br>";
var o = $('#outputWindow');
var i = $('#inputWindow');
var s = $('#sendButton');
var t = $('#typing');
var r = 0;
//arrays
var msg = ['msg1<br />', 'msg2<br />', 'msg3<br />'];
//fire send events
$(s).click(function() {
runAI();
});
$(i).keydown(function(e) {
if (e.keyCode == 13) {
runAI();
}
});
function runAI() {
if (i.val().length > 0) {
r = r + 1;
o.html(o.html()+n+$("#inputWindow").val()+"<br><hr>" );
setTimeout(function(){ $("#typing").show(); }, 3000);
i.val('');
i.focus();
}
}
i.focus();
});
答案 0 :(得分:2)
<!DOCTYPE html>
<html lang="en">
<title>Automated Chat</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.js'></script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function() {
$("#typing").hide();
var n = "You:<br>";
var o = $('#outputWindow');
var i = $('#inputWindow');
var s = $('#sendButton');
var t = $('#typing');
var r = 0;
//arrays
var msg = ['msg0<br />','msg1<br />', 'msg2<br />', 'msg3<br />'];
//fire send events
$(s).click(function() {
i.hide();
s.hide();
runAI();
});
$(i).keydown(function(e) {
if (e.keyCode == 13) {
runAI();
i.hide();
s.hide();
}
});
function runAI() {
if (i.val().length > 0) {
r = r + 1;
o.html(o.html()+n+$("#inputWindow").val()+"<br><hr>" );
setTimeout(function(){ $("#typing").show(); }, 1000);
setTimeout(function(){ o.html(o.html()+"Username:<br>"+msg[r]+"<hr>") }, 3000);
setTimeout(function(){ $("#typing").hide();i.show();s.show();i.val(''); i.focus(); }, 3000);
}
}
i.focus();
});
});//]]>
</script>
</head>
<body>
<div class="container">
<div id="wrapper">
<div id="outputWindow"></div>
<input type="text" id="inputWindow"></input>
<input type="submit" id="sendButton" value="Send Message"/>
<div id="typing">Username is typing..</div>
</div>
</body>
</html>
我认为已经完成了。
答案 1 :(得分:0)
我发现问题是什么,我在OP中设置了r = 0。 我继续将其更改为-1,现在它不再跳过第一个响应了。
这是因为它在OP中从1而不是0开始计数。