![](/img/trans.png)
[英]Javascript code to perform split() on user names entered in input field
[英]Perform javascript code based on user input
我正在使用javascript和jQuery开发自己的游戏。 现在,我为用户提供了一种在字段中键入内容的方法,并在按Enter时将其放入“文本框”(实际上只是一个div)。
我的功能如下:
function input_field() {
var inputField = $('#dialog-input');
var messageField = $('#dialog-text');
inputField.keyup(function(e) {
if (e.keyCode === 13) {
window.userInput = inputField.val();
// Check if the input area is not empty (prevents adding white lines)
if(userInput !== '') {
messageField.append('You: ' + window.userInput + '<br>');
inputField.val('');
messageField.scrollTop(999999999);
return true;
} else {
return false;
}
}
});
}
我还有一个向用户显示消息的功能,其功能如下:
function cAlert(mssg) {
var message = mssg;
var messageField = $('#dialog-text');
messageField.append(message + '<br>');
messageField.scrollTop(999999999);
}
这里的问题是我想将其变成一个问题+答案系统,在该系统中,系统会提一个问题(通常是“您进入房间,您会做什么?”的形式),而用户会提供一个答案(看周围)。
我知道我可以使用prompt
执行此操作,然后使用switch case
检查结果,但这会使我的代码看起来非常丑陋且不可重用。
我的问题是:如何使系统等待用户输入,然后根据给定的信息执行操作?
我认为您必须在输入字段中注册一个onChange
侦听器:
$('#dialog-input').onChange(function() {
// Handle user's input
});
这样,当用户按下Enter
或离开该字段时,系统将收到通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.