我试图在用户输入时从文本框中获取文本,以便我可以解析它并在用户输入命令时相应地显示信息。但是,似乎我正在编写的函数是在将字母输入文本框之前从框中获取文本。如何防止函数在输入键入的字符之前从文本框中抓取内容?我考虑获取密钥的 id 并相应地更改输入的字符串,但我觉得应该有更好的方法。
编码:
$('#inputConsoleForm').keydown(function(event){
//Get key code
var code = (event.keyCode ? event.keyCode : event.which);
//Get console text (doesn't behave as expected)
var consoleCommand = document.inputConsoleForm.console.value;
function parseConsoleCommand(consoleCommand) {
/* Returns true if command is valid*/
}
if(code === 13) {
event.preventDefault();
if(!parseConsoleCommand(consoleCommand))
alert("INVALID COMMAND LINE");
else
attemptExecute();//Runs the command
}
if(code === 32 || (code >= 48 && code <= 123) || code === 61 || code === 109 || code === 188 || code === 8) {
if(parseConsoleCommand(consoleCommand)){
$(document.inputConsoleForm.console).css("background-color", "#FFDFDF");
}
else{
$(document.inputConsoleForm.console).css("background-color", "");
}
}
});