简体   繁体   English

在不使用提示的情况下在javascript中获取用户输入

[英]Getting user input in javascript without using prompt

I have a setup where I allow the user to type commands in a regular html textfield. 我有一个设置,允许用户在常规的html文本字段中键入命令。 I handle the onkeypress event on the textfield, check if it is an enter key that has been pressed, and parse and execute the command. 我在文本字段上处理onkeypress事件,检查它是否是已按下的Enter键,然后解析并执行命令。

An example could be: 例如:

deletefile /testfolder/testfile.html

The code kind of looks like this: 代码看起来像这样:

inputControl.onkeypress = function(e) { 
    if (e.which == 13) { 
        var outputMessage = commandHandler.executeCommand(inputControl.value);
        printOutputToScreen(outputMessage);
        inputControl.value = "";
        inputControl.focus();
    }
}

(by the way this is not what my code actually looks like, it's just an example) (顺便说一下,这并不是我的代码真正的样子,这只是一个示例)

The idea here is to be able to write a command, see the output, write another command, see the output, etc. 这里的想法是能够编写命令,查看输出,编写另一个命令,查看输出等。

executeCommand parses the commandstring and calls a specific command with the appropriate arguments. executeCommand解析命令字符串,并使用适当的参数调用特定命令。

> deletefile /test1.html
The file was deleted

> deletefile /test2.html
An error occured while trying to delete the file

This part is simple. 这部分很简单。 What I need help with is this: If the user just types "deletefile", I want to be able to ask the user (using the outputarea) "Which file do you want to delete?". 我需要帮助的是:如果用户仅键入“ deletefile”,我希望能够(使用outputarea)询问用户“您要删除哪个文件?”。

I know I can do this with: 我知道我可以做到:

var additionalInput = prompt("Which file?");

, but I don't want to use the prompt. ,但我不想使用提示。

Forgetting about my contrived example, does anyone know of a good way of asking the user for input in the middle of a javascript (not using prompt), like: 忘了我做作的例子,没有人知道在javascript中间要求用户输入的一种好方法(不使用提示),例如:

var status = doSomeStuff(args);  
if (status.fail) {
    tmp = getUserInput();  
    doSomeFinalStuffBasedOnTheUserInput(tmp);
}

Is this even possible? 这有可能吗?

I'm not sure I'm able to explain myself here, but if anyone feel they can help me in some way please let me know :-) 我不确定我是否可以在这里解释自己,但是如果有人认为他们可以以某种方式帮助我,请告诉我:-)

One solution could be to implement a global state variable and use this as a mechanism in the keypress-eventhandler to route the input to the command, something like: 一种解决方案是实现全局状态变量,并将其用作keypress-eventhandler中的一种机制,以将输入路由到命令,例如:

inputControl.onkeypress = function(e) {
  if (e.which == 13) {
      if (state.additionalInputPending && currentCommand != null) {
          currentCommand.executeWithAdditionalInput();
      } else {
          // perform regular command execution
      }
  }
}

In this way my "app" needs to handle in internal state variable, and store the currentCommand between keypresses. 这样,我的“ app”需要处理内部状态变量,并在按键之间存储currentCommand。

You could use a jquery/javascript solution where you show a 'fancy' dialog (not a JS prompt) to ask for the input. 您可以使用jquery / javascript解决方案,在其中显示“花式”对话框(而不是JS提示)以要求输入。

http://trentrichardson.com/Impromptu/#Examples The very last example shows forms. http://trentrichardson.com/Impromptu/#Examples最后一个示例显示表单。 Impromptu is just one of many (many, many) solutions for dialogs/prompt alternatives. Impromptu只是许多对话框/提示替代方法的解决方案之一。

The only other option that comes to mind is to do it directly in the JS console you're creating more like a real command-line tool. 想到的唯一其他选择是直接在您正在创建的JS控制台中执行此操作,更像是一个真正的命令行工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM