保持即兴“向上” 执行 jquery Ajax/MVC post 时

发布于 2024-08-01 16:09:42 字数 2521 浏览 4 评论 0原文

有没有办法在发帖过程中保持即兴对话框的显示?
这是 javascript 代码

$('#linkPostTest').click(function() {        
      openprompt();
});

function openprompt() {
    var temp = {
        state0: {
            html: 'Are you sure you want to post?<br />',
            buttons: { Yes: true, No: false },
            focus: 1,
            submit: function(v, m, f) {
                if (v) {
                    var form = $('frmPostTest');
                    $.ajax(
                            {
                                type: 'POST',
                                url: '/Path/TestPost',
                                data: form.serialize(),
                                success: function(data) {
                                            // I realize I could check "data" 
                                            // for true...just have not 
                                            // implemented that yet....
                                    $.prompt.goToState('state1');
                                    //$.prompt('Test was successful!');
                                },
                                error: function() {
                                    $.prompt.goToState('state2');
                                    //$.prompt('Test was not successful.');
                                }
                            }
                        );

                    return true;
                }
                else {
                    //$.prompt.goToState('state1'); //go forward
                    return false;
                }
            }
        },
        state1: {
            html: 'Test was successful!',
            buttons: { Close: 0 },
            focus: 0,
            submit: function(v, m, f) {
                if (v === 0) {
                    $.prompt.close();
                }
            }
        },
        state2: {
            html: 'Test was not successful.<br />',
            buttons: { Close: 0 },
            submit: function(v, m, f) {
                if (v === 0) {
                    $.prompt.close();
                }
            }
        }
    };
    $.prompt(temp);
}

当我点击“是”时,控制器会执行此

[AcceptVerbs(HttpVerbs.Post)]
    public bool TestPost()
    {            
        // runs some code that saves some data...
        // this works fine
        bool updated = functionThatSavesCode();
        return updated;
    }

操作“您确定要发帖吗?” 显示临时对话框...它消失...我怎样才能让它保持显示?

Is there a way to keep the impromptu dialog box displayed during a post?
Here's the javascript code

$('#linkPostTest').click(function() {        
      openprompt();
});

function openprompt() {
    var temp = {
        state0: {
            html: 'Are you sure you want to post?<br />',
            buttons: { Yes: true, No: false },
            focus: 1,
            submit: function(v, m, f) {
                if (v) {
                    var form = $('frmPostTest');
                    $.ajax(
                            {
                                type: 'POST',
                                url: '/Path/TestPost',
                                data: form.serialize(),
                                success: function(data) {
                                            // I realize I could check "data" 
                                            // for true...just have not 
                                            // implemented that yet....
                                    $.prompt.goToState('state1');
                                    //$.prompt('Test was successful!');
                                },
                                error: function() {
                                    $.prompt.goToState('state2');
                                    //$.prompt('Test was not successful.');
                                }
                            }
                        );

                    return true;
                }
                else {
                    //$.prompt.goToState('state1'); //go forward
                    return false;
                }
            }
        },
        state1: {
            html: 'Test was successful!',
            buttons: { Close: 0 },
            focus: 0,
            submit: function(v, m, f) {
                if (v === 0) {
                    $.prompt.close();
                }
            }
        },
        state2: {
            html: 'Test was not successful.<br />',
            buttons: { Close: 0 },
            submit: function(v, m, f) {
                if (v === 0) {
                    $.prompt.close();
                }
            }
        }
    };
    $.prompt(temp);
}

The controller does this

[AcceptVerbs(HttpVerbs.Post)]
    public bool TestPost()
    {            
        // runs some code that saves some data...
        // this works fine
        bool updated = functionThatSavesCode();
        return updated;
    }

After I click Yes when the 'Are you sure you want to post?' impromptu dialog is displayed... it disappears...How can I make it stay displayed?

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

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

发布评论

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

评论(1

旧城烟雨 2024-08-08 16:09:42

好的,它可以工作了...即兴插件和 jQuery 给我留下了深刻的印象!
为了使其正常工作,我做了两件事:

return false;

在 state0 块下添加两个语句,然后...

将 ajax 调用设置为

async: false,

以下是新的 javascript:

$('#linkTestPost').click(function() {
    TestPost();
});

function TestPost() {
    var temp = {
        state0: {
            html: 'Are you sure you want to post?<br />',
            buttons: { Yes: true, No: false },
            focus: 1,
            submit: function(v, m, f) {
                if (v) {
                    if (PostView() === true) {
                        $.prompt.goToState('state1');
                        // the line below was missing from my original attempt
                        return false;
                    }
                    else {
                        $.prompt.goToState('state2');
                        // the line below was missing from my original attempt
                        return false;
                    }
                }
                else {                        
                    return false;
                }
            }
        },
        state1: {
            html: 'Test Post was successful!',
            buttons: { Close: 0 },
            focus: 0,
            submit: function(v, m, f) {
                if (v === 0) {
                    $.prompt.close();
                }
            }
        },
        state2: {
            html: 'Test Post was not successful',
            buttons: { Close: 0 },
            submit: function(v, m, f) {
                if (v === 0) {
                    $.prompt.close();
                }
            }
        }
    };
    $.prompt(temp);
}

function PostView() {
    var form = $('frmTestPost');
    var postSuccess = new Boolean();        
    $.ajax(
    {
        type: 'POST',
        url: '/Path/TestPost',
        data: form.serialize(),
        // the line below was missing from my original attempt
        async: false,            
        success: function(data) {
            postSuccess = true;
        },
        error: function() {
            postSuccess = false;
        }
    });        
    return postSuccess;
}

OK got it to work...I'm really impressed with the impromptu plug-in and jQuery!
Two of the things I did differently to get this to work was to add the two

return false;

statements under the state0 block and...

to set the the ajax call to

async: false,

Here's the new javascript:

$('#linkTestPost').click(function() {
    TestPost();
});

function TestPost() {
    var temp = {
        state0: {
            html: 'Are you sure you want to post?<br />',
            buttons: { Yes: true, No: false },
            focus: 1,
            submit: function(v, m, f) {
                if (v) {
                    if (PostView() === true) {
                        $.prompt.goToState('state1');
                        // the line below was missing from my original attempt
                        return false;
                    }
                    else {
                        $.prompt.goToState('state2');
                        // the line below was missing from my original attempt
                        return false;
                    }
                }
                else {                        
                    return false;
                }
            }
        },
        state1: {
            html: 'Test Post was successful!',
            buttons: { Close: 0 },
            focus: 0,
            submit: function(v, m, f) {
                if (v === 0) {
                    $.prompt.close();
                }
            }
        },
        state2: {
            html: 'Test Post was not successful',
            buttons: { Close: 0 },
            submit: function(v, m, f) {
                if (v === 0) {
                    $.prompt.close();
                }
            }
        }
    };
    $.prompt(temp);
}

function PostView() {
    var form = $('frmTestPost');
    var postSuccess = new Boolean();        
    $.ajax(
    {
        type: 'POST',
        url: '/Path/TestPost',
        data: form.serialize(),
        // the line below was missing from my original attempt
        async: false,            
        success: function(data) {
            postSuccess = true;
        },
        error: function() {
            postSuccess = false;
        }
    });        
    return postSuccess;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文