Jquery - 等待用户输入 [英] Jquery - waiting for user input

查看:80
本文介绍了Jquery - 等待用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个弹出窗口来确认用户操作(例如删除表格中的一行......)。旨在使弹出窗口完全HTML和Javascript。我无法理解的是我如何让javascript等待用户输入,但仍然可以用于其他javascript事件。 (其他点击触发的东西等...)。我认为我会做的就像

im trying to make a popup that comes up to confirm a users action (deleting a row in a table, for example..). aiming at making the popup entirely HTML and Javascript. What i can't get my head around is how i would make the javascript wait for the users input, but still function for other javascript events. (other click triggered stuff, etc..). What i thought i'd do is something like

if(makePopup("Are you sure?") == true) {
 //bla bla 
}

其中makePopup函数会创建一个包含弹出窗口的弹出窗口传递给它的字符串,并为用户提供yes或no选项。在该函数的某处,我需要一个等待用户单击是或否的循环。代码只是在那一行等待,直到完成某事。这就是我想要的...但是我的问题..如果代码卡在那里的循环中,jquery如何执行其他任务?例如,如果弹出框可以拖动怎么办?当代码忙于循环时,如何触发这些效果的事件..?现在我无法理解解决方案..

where makePopup function would create a popup containing the string passed to it, and give the user a yes or no option. somewhere in that function i'd need a loop that would wait for the user to click yes or no. the code just waits at that line until something is done. which is what i want... but theres my problem.. if the code is stuck in a loop there, how can jquery perform other tasks? for example, what if the popup box was draggable? how will the events for these effects get trigged when the code is busy looping at that point..? just cant get my head around a solution right now..

这是我目前要测试的东西..

here's something i have at the moment to test..

        $(document).ready(function(){
        $('a').click(function(){
            makePopup("Wana go to google?",function(){
                window.location.replace("http://www.google.com");
            });
        });
    });

    function makePopup(msg,callback){
        $('body').append("<div class='popup'><p>"+msg+"</p></div>");
    }

仍然不确定如何进行确认。弹出窗口应该包含是和否按钮。

still unsure of how to do the confirmation stuff. the popup should include a yes and no button.

推荐答案

你可以实现类似这样的东西

you can implement something like this

makePopup("Are you sure?", function {
  //bla bla 
});

只有在用户确定,点击按钮或其他任何内容后才会调用回调。 Smth喜欢:

which will call your callback only if user sure, after he clicks a button or whatever. Smth like:

function makePopup(msg, callback) {
     $("#sureformmsg").html(msg);
     $("#sureform").show().submit(function() { if (..check..) callback(); });
}






你的例子:


Your example:

$(document).ready(function(){
    $('a').click(function(){
        makePopup("Wana go to google?",function(){
            window.location.replace("http://www.google.com");
        });
    });
});

function makePopup(msg,callback){
    $('body').append("<div class='popup'><p id='themsg'>"+msg+"</p></div>");
    $('#themsg').onclick(callback);
}

这篇关于Jquery - 等待用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆