如何在Impromptu回调中传递参数?

时间:2011-05-05 03:49:13

标签: jquery impromptu

我正在使用jQuery Impromptu作为弹出窗口。原始代码如下:

function f1(pa1, pa2){
    $.prompt("need to delete?",{
         callback: callbackfunc,
         button: {OK: true, Cancel: false}
    });
}
function callbackfunc(v,m,f){
    if(v){
        // want to process the pa1 and pa2.
    }
}

如何在回调中处理pa1pa2

4 个答案:

答案 0 :(得分:1)

我在项目中使用你想要的东西。见:

function removePhoto(id){
    var txt = 'Are you sure you want to remove this photo?<input type="hidden" id="photoid" name="photoid" value="'+ id +'" />';

    $.prompt(txt,{ 
        buttons:{Delete:true, Cancel:false},
        callback: function(v,m,f){
            if(v){
                var pid = f.photoid;

                //Here is where you would do an ajax post to remove the user
                //also you might want to print out true/false from your .php
                //file and verify it has been removed before removing from the 
                //html.  if false dont remove, $promt() the error.
                $.get('<?php echo base_url(); ?>index.php/ajaxed/del_photo/'+pid, function(data){
                    if(data != 'false'){
                        $.prompt('The photo was successfuly deleted...', { callback: function() {
                            photosEdit--;
                            if(photosEdit == 0) {
                                window.location = "<?php echo base_url(); ?>index.php<?php echo ($row_p->aid == 0 ? "/gallery/view/me" : "albums/view/".$this->dx_auth->get_user_id()."/".$row_p->aid); ?>";
                            } else {
                                $("div#editWrapper_"+pid).hide("slow", function() { $(this).remove();});
                            }
                        } });
                        //recirect history-1
                    }else{ $.prompt('An error occured while removing this photo...'); }                         
                });
            }
            else{}
        }
    });
}

答案 1 :(得分:0)

我这样解决了我的问题:

function f1(pa1, pa2){
   $.prompt("need to delete?",{
       callback: function(v, m, f){
            if(v){
               //  process of pa1, pa2...
            }
       },
       button: {OK: true, Cancel: false}
   });
}

如果要处理的参数较少,则可以。但是,如果回调函数太复杂,而且参数很多,这个方法就不好了。

我希望有人能给我一个更好的建议。

答案 2 :(得分:0)

如果回调方法的代码变得复杂,最好的方法是:

var g_pa1,g_pa2;  // General variables

function f1(pa1, pa2){

    g_pa1 = pa1;
    g_pa2 = pa2;

    $.prompt("need to delete?",{
     callback: callbackfunc,
     button: {OK: true, Cancel: false}
    });
}

function callbackfunc(v,m,f){
    if(v){
        //PROCESS YOUR g_pa1 and g_pa2
        g_pa1 = null;
        g_pa2 = null;
    }
}

答案 3 :(得分:0)

我不确定插件是否改变了这个或某处有错误的文档,但callbacksubmit nowadays and it takes 4 arguments而不是3: event, value, message, formVals

buttons: { Cancel: false, Next: true },
submit:function(e,v,m,f){ }

改编@Oytun Tez excelent answer,我做了这个例子:

<html>
<head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="impromptu.js"></script>
    <link rel="stylesheet" media="all" type="text/css" href="impromptu.css" />

    <script type="text/javascript">
    function removePhoto( id )
    {
        var txt = 'Are you sure you want to remove this photo?<input type="hidden" id="photoid" name="photoid" value="'+ id +'" />';
        $.prompt( txt,
        { 
            title: "Remove Photo",
            buttons:{ Delete:true, Cancel:false },
            submit: function( event, value, message, formVals )
            {
                if( value )
                {
                    var pid = formVals.photoid;
                    console.log('pid: ' + pid );
                }
            }
        });
    }

    $(document).ready( function() 
    {
        $('.btn').on('click', function( e )
        {
            removePhoto( $(this).data('id') ); 
        });
    });
    </script>
</head>
<body>
    <input class="btn" type="button" value="Remove photo 1" data-id="1" />
    <input class="btn" type="button" value="Remove photo 2" data-id="2" />
</body>
</html>