简体   繁体   English

jQuery 确认对输入文本的效果

[英]jQuery confirm like effect on input text

Well, here is what I wanna do: I have a text field and a textarea that both act as a console window. I am developing a very basic simulation of an interpreter with its own programming language with javascript (jQuery), so when I start executing a program that the user has previously typed, I go through all the instructions and when an special instruction is reached ("leer") The user has to type something into this console and pause the execution of the program, I know that the user is done typing when the 'return' key is pressed.好吧,这就是我想做的:我有一个文本字段和一个文本区域,它们都充当控制台 window。我正在开发一个非常基本的解释器模拟,它有自己的编程语言 javascript (jQuery),所以当我开始执行用户之前输入的程序,我 go 通过所有指令,当到达特殊指令(“leer”)时,用户必须在此控制台中输入一些内容并暂停程序的执行,我知道用户按下“返回”键时完成输入。 When that happens the program continues its normal execution flow.当发生这种情况时,程序将继续其正常的执行流程。

Now, this behavior is quite similar to the one of a confirmation box, because it waits for an input and when the user presses any button (yes/no) the execution continues.现在,此行为与确认框非常相似,因为它等待输入,当用户按下任何按钮(是/否)时,执行将继续。

I hope I have explained clear enough so that all of you guys know what my problem is.我希望我已经解释得足够清楚,以便你们所有人都知道我的问题是什么。 I know it would be great to place some code here, but I don't really know what part of it would help, so in case you need some chunk of code just let me know.我知道在这里放置一些代码会很棒,但我真的不知道它的哪一部分会有帮助,所以如果您需要一些代码块,请告诉我。

Here is how the page looks like.这是页面的样子。chmaquina 执行

and here is the code used to execute every single program:这是用于执行每个程序的代码:

this.ejecutarPrograma = function(pidprograma) {
    var programa = this.listaProgramas[pidprograma];
    if(programa == undefined) throw "ERROR!, PID no encontrado"
    var posIni, posEnd;
    posIni = programa.obtenerRbp();
    posEnd = programa.obtenerRlc();
    this.cp = posIni;
    var temp = this.memoria[this.cp].split(/\s+/);
    while (this.cp < posEnd || temp[0] != "retorne") {
        try {
            if (temp[0] == "vaya") {
                this.cp = ACCIONES[temp[0]](this.cp, temp[1], programa);
            }
            else if (temp[0] == "vayasi") {
                this.cp = ACCIONES[temp[0]](this.memoria[0], this.cp, temp[1], temp[2], programa);
            }
            else if (temp[0] == "nueva" || temp[0] == "etiqueta" || temp[0] == "retorne") {
                // nada de nada por aca porque estos ya estan implementados
                this.cp++;
            }
            else {
                this.memoria[0] = ACCIONES[temp[0]](this.memoria[0], temp[1], programa);
                this.cp++;
            }
            var temp = this.memoria[this.cp].split(/\s+/);
        }
        catch (e) {
            throw "Linea " + (this.cp).toString() + ": " + e;
        }
    }
};

ACCIONES is an associative array that keeps the names of the instruction and as a value has the function, the instruction "leer" is empty it just gets a value from the input and copies that value in a variable of the program ACCIONES 是一个关联数组,它保留指令的名称并且作为一个值具有 function,指令“leer”为空它只是从输入中获取一个值并将该值复制到程序的一个变量中

Thanks for everyone's help.感谢大家的帮助。

I'm not sure I completely understand the question, but it sounds like you're looking for the prompt function.我不确定我是否完全理解这个问题,但听起来您正在寻找prompt function。

For example:例如:

prompt('Please enter your input.');

After the user clicks OK, the input is returned as a string.用户单击“确定”后,输入将作为字符串返回。 If they click cancel, null is returned.如果他们单击取消,则返回 null。

Edit:编辑:

After seeing your comments on imsky's answer, I think I have a better understanding of what you want to do.看到你对 imsky 的回答的评论后,我想我对你想做什么有了更好的理解。 Correct me if I'm wrong.如果我错了纠正我。

You have a loop that you want to run continuously until the user does something, at which point you want your loop to wait for the user to finish, then continue execution of the loop?你有一个循环,你想连续运行直到用户做某事,此时你希望你的循环等待用户完成,然后继续执行循环?

If this is the case, try putting your loop in a function and use setTimeout(yourfunction,0) ;如果是这种情况,请尝试将循环放入 function 并使用setTimeout(yourfunction,0) Every iteration of the function, check if the user has done something (which means setting a variable when the user does something and checking it from within yourfunction() . If the user hasn't done something, continue looping (by using the setTimeout trick), otherwise do whatever you need to, after which you can again call yourfuntion() . The only catch here is that you can't interrupt yourfunction in the middle of exectution, just inbetween each "loop". function 的每次迭代,检查用户是否做了某事(这意味着当用户做某事时设置一个变量并从yourfunction()中检查它。如果用户没有做某事,继续循环(通过使用 setTimeout 技巧),否则做任何你需要做的事,之后你可以再次调用你的yourfuntion() 。这里唯一的问题是你不能在执行过程中中断你的yourfunction ,就在每个“循环”之间。

Here's an example: http://jsfiddle.net/PYabX/这是一个例子: http://jsfiddle.net/PYabX/

Does this make sense, or am I completely off?这有意义吗,还是我完全离开了?

It sounds like you want the keyup handler on your console input field.听起来您想在控制台输入字段上使用keyup处理程序。 Check the key code of the event and if it's the Enter key, keep processing the program.检查事件的键码,如果是回车键,则继续处理程序。

Edit: OK, I see that you have a while loop in your code.编辑:好的,我看到你的代码中有一个 while 循环。 Try introducing another while loop when you hit the input instruction, and satisfy it once Enter is hit.当您按下输入指令时尝试引入另一个 while 循环,并在按下 Enter 后满足它。

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

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