最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery - waiting for user input - Stack Overflow

programmeradmin8浏览0评论

im trying to make a popup that es 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 
}

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("");
            });
        });
    });

    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.

im trying to make a popup that es 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 
}

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.");
            });
        });
    });

    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.

Share Improve this question edited Jan 9, 2010 at 10:48 Prodigga asked Jan 9, 2010 at 10:17 ProdiggaProdigga 1,4872 gold badges22 silver badges38 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

you can implement something like this

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

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.");
        });
    });
});

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

评论列表(0)

  1. 暂无评论