Jquery Impromptu函数运行顺序问题

时间:2011-03-30 09:02:46

标签: javascript jquery

我有一个使用$ .prompt Jquery(即兴)插件的函数。这种方法很好,只要每次调用函数时它都会在调用它的函数末尾运行。

这是函数......

function addfile(){

    var txt = '<?php echo $JSString; ?>';

    function mycallbackform(v,m,f){
        if(v != undefined)
        var newText = f.alertName + " , " + f.alertName.replace("-", " ").replace(".php", "");
        alert(newText);
    }

    $.prompt(txt,{
        callback: mycallbackform,
        buttons: { Add: 'add', Cancel: 'cancel' }
    });
}

PHP位只是添加了html字符串并且工作正常,使用文本时问题仍然存在(即'这是一个提示')。


每当我从JS调用addfile()时它都会运行到最后。例如...

function newfunction()
{
       prompt("Before");
       addfile();
       prompt("after");
}

...将显示以下内容......

  1. 提示 - '之前'
  2. 提示 - '之后'
  3. addfile();

  4. 无论我做什么,addfile()总是会运行最后让我感到困惑。我对这些事情都很陌生,所以如果我做任何非常愚蠢的事情,请不要害怕指出它。

    小写:这个功能坐在我的标题中php文件,以便<?php echo $JSString; ?>正常工作。我已经删除了php并将该函数插入到外部JS文件中,但同样的问题占上风,因此它是$ .prompt,这似乎导致问题而不是JS


    任何关于如何让这个JS表现得非常感激的想法。非常感谢

2 个答案:

答案 0 :(得分:1)

你的提示函数是异步的,这就是为什么它接受一个回调函数作为参数。

试试这个:

prompt(
    txt,
    {callback: function() {
        addfile();
        prompt("after");
    }}
);

答案 1 :(得分:1)

以上方法无效。下面是代码部分。在阻止页面之前显示警报。我正在使用blockUI

function blockPage(){
    $.blockUI({
        message: "<img src='loading.gif'/>",
        color: 'transparent',
        backgroundcolor: '#777777',
        top: $(window).height()/2
    });
    return true;
}

function dosomethingelse(){
    var aa;
    var bb;
    alert("c");
}

function createOverlay(){
    var overlaySubmit = function(e,v,m,f){
        if(v === -1){
            alert("Closing the prompt");
            $.prompt.close();
            return false;
        } else if(v === 1){
            dosomethingelse();
            //blockPage();
            return false;
        }
    };
    var prompt = $.prompt(
        "This is a test prompt", 
        {
            opacity: 0.3,
            buttons: {Add: 1, Cancel: -1},
            position: {x: 300},
            prefix: 'jqi',
            submit: function(e,v,m,f){
                blockPage();
                try{
                    alert(g);
                } catch (err){
                    alert("here");
                }
                overlaySubmit(e,v,m,f);
            }
        });
    prompt.bind('promptsubmit',blockPage);
}
<!-- Ajax-->
$('#ajax').click(function(){
    $.ajaxSetup({async:false});
    $.ajax({
            url: "test-server.txt",
            success: function(response){
                createOverlay();
                return false;
            },
            error: function(){
                return false;
            }
    });
});