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

发布于 2024-11-05 07:49:51 字数 354 浏览 3 评论 0原文

我使用 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

I'm using jQuery Impromptu as a pop window. The raw code is as follow:

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.
    }
}

How to process pa1 and pa2 inside the callback?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡笑忘祈一世凡恋 2024-11-12 07:49:51

我在项目中使用你想要的东西。看看这个:

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{}
        }
    });
}

I use what you want in a project. See this:

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{}
        }
    });
}
梦里梦着梦中梦 2024-11-12 07:49:51

我这样解决我的问题:

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

如果需要处理的参数较少就可以了。但是,如果回调函数过于复杂,参数较多,则此方法不太好。

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

I solve my problem like this:

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

It's OK if there is less parameters to process. however, if the callback function is too complex, and there are much parameters, this method is a not good.

I hope somebody could give me a better suggestion.

指尖上的星空 2024-11-12 07:49:51

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

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;
    }
}

If your code on the callback method is getting complex, the best way to do this is:

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;
    }
}
静若繁花 2024-11-12 07:49:51

我不确定插件是否改变了这一点,或者某个地方有错误的文档,但是 callbacksubmit 如今,它需要 4 个参数,而不是 3 个:event, value, message, formVals

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

改编@Oytun Tez优秀答案,我已经做了这个例子:

<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>

I'm not sure if the plugin changed this or there was a wrong documentation somewhere, but the callback is submit nowadays and it takes 4 arguments instead of 3: event, value, message, formVals:

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

Adapting @Oytun Tez excelent answer, I've made this example:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文