大家好,我一直在创建一个小聊天机器人(为了好玩和练习)。
我有以下函数不能正常工作(FULL CODE HERE):
function runAI() {
if (i.val().length > 0) {
if ($.inArray(i.val(), helloInputArray)) {
r = Math.floor(Math.random()*4);
o.html(o.html()+helloOutputArray[r]);
i.val('');
i.focus();
} else if ($.inArray(i.val(), byeInputArray)) {
r = Math.floor(Math.random()*4);
o.html(o.html()+byeOutputArray[r]);
i.val('');
i.focus();
} else {
o.html(o.html()+"I don't know what that means...<br />");
i.val('');
i.focus();
}
}
}它似乎总是返回helloOutputArray...
发布于 2010-09-15 18:17:25
$.inArray不返回true或false,它返回一个从0开始的索引。
-1表示未找到,-1 \f25>-1是数组中匹配的索引:
if ($.inArray(i.val(), helloInputArray) > -1) {
// The item was in this array
}Working version here.
https://stackoverflow.com/questions/3716656
复制相似问题