无法创建由ajax加载的简单html增强模式对话框

发布于 2024-12-21 03:22:00 字数 4417 浏览 5 评论 0原文

我的应用程序中有一些类似向导的页面,它在保持状态的同时 在小步骤之间导航,导航不是线性的,一切都 以“渐进增强”方式无需一行 JavaScript 即可正常工作。

在我的应用程序中,对于启用了 javascript 的用户,我想要转流 通过 ajax 加载对话框中的每个完整步骤,在一组对话框中进行上述操作, 处理步骤的动作并关闭对话框,每个步骤都有自己的 嵌入的脚本在对话框加载时执行并拦截一些步骤 ui 事件。

问题是 JQuery UI 对话框想要创建操作按钮,但我不能 将按钮创建交给插件,它要求按钮元数据和我 已经拥有完整的功能页面,包括表单、按钮、数据输入和 我工作所需的一切,它正在工作,它完成了,我只想加载它 在一个对话框上管理窗口特定的东西给我,比如标题栏,拖动 标题栏,标题栏上的关闭按钮,关闭我的清理事件,拉伸到 适合我的对话框内容,以覆盖模式模式加载。

我找不到通过脚本拦截对话框内按钮单击的方法 嵌入对话框步骤,对话框内的按钮必须通过ajax发布数据,但是 它将在正常的页面发布中一起发布主页。

我发现了一些非常旧的插件,但我喜欢 JQuery UI,它简单而且看起来 好,寻找没有 iframe 的东西,我读到:

boxy: http://onehackoranother.com /projects/jquery/boxy/tests.html

simplemodal: http://www.ericmmartin.com/projects/simplemodal/

jqModal:http://dev.iceburg.net/jquery/jqModal/

@liho1eye:现在发表评论

编辑:在@liho1eye的帮助下我达到了它:

<script type="text/javascript">
    //-------------------------------------------------
    var url_trg = '@Url.Content("~/Teste/opendialog")';
    var url_prl = '@Url.Content("~/Images/waitplease.gif")';
    //-------------------------------------------------
    function onloadpartial() {
        /*setupDialog("#opendialog", "#tempcontent", "section[id='main']", url_trg);*/
        configDetailDialog(url_trg, "#tempcontent", "section[id='main']", "Detail", "#opendialog");
    }
    //-------------------------------------------------
    function configDetailDialog(trgurl, containerselector, contentselector, dlgtitle, buttonselector) {
        //-------
        $(document).ajaxError(
            function (event, jqXHR, ajaxSettings, thrownError) {
                alert('[event:' + event + '], ' +
                        '[jqXHR:' + jqXHR + '], ' +
                        '[jqXHR_STATUS:' + jqXHR.status + '], ' + 
                        '[ajaxSettings:' + ajaxSettings + '], ' +
                        '[thrownError:' + thrownError + '])');
            });
        //-------
        $.ajaxSetup({ cache: false });
        //-------
        $(buttonselector).click(function (event) {
            event.preventDefault();
            openAjaxDialog(trgurl, containerselector, contentselector, dlgtitle);
        });
        //-------
    }
    //-------------------------------------------------
    function openAjaxDialog(trgurl, containerselector, contentselector, dlgtitle) {
        $.ajax({
            type: 'GET',
            url: trgurl,
            context: document.body,
            success: function (data) {
                var dlg = $(data).find(contentselector);
                $('#dlgdetail').remove();
                $(containerselector).append("<div id='dlgdetail'/>");
                $('#dlgdetail').append(dlg);
                $('#dlgdetail')
                    .css("border", "solid")
                    .dialog({
                        autoOpen: true,
                        modal: true,
                        title: dlgtitle,
                        open: function () {
                            configDetailDialog();
                        },
                        close: function (event, ui) {
                            $('#dlgdetail').remove();
                        }
                    })
                    .find("form").submit(function (event) {
                        alert('clicou ' + event);
                        var form = $(this);
                        var faction = "http://" + window.location.host + trgurl;
                        var fdata = form.serialize() + "&action:savedialog=savedialog";
                        $.ajax({                            
                            type: "POST",
                            url: faction,
                            data: fdata,
                            success: function (result) {
                                alert(result);
                            }
                        });
                        event.preventDefault();
                        $('#dlgdetail').dialog('close');
                    });
            }
        });
    }
    //-------------------------------------------------
</script>
-------------------------------------------------

I have some wizard-like pages in my application, its keeping state while
navigating between small steps, the navigation is not linear and everything
works well without one line of javascript in a "progressive enhancement" way.

In my application, to users with javascript enabled, i want turn flow
above in a set of dialogs by loading each complete step in a dialog by ajax,
process the action of step and close dialog, each step will have the own
script embedded to execute on dialog load and intercept some step ui events.

The problem is that JQuery UI Dialog want create action buttons i cant
give the button creation to plugin, its ask for buttons metadata and i
already have complete functional page with form, buttons, data entry and
everything i need to do my work, its working, its done, i Only want load it
on a Dialog that manage window specific things to me, like Title bar, Drag by
title bar, Close button on title bar, Close event to my cleanup, stretch to
fit my dialog content, load in modal mode with overlay.

I cant find a way to intercept the button click inside dialog by script
embedded on dialog step, the button inside dialog must post data by ajax but
it will post togheter the main page in a normal page posting.

I found some very old plugins but i like JQuery UI, its simple and looks
good, looking for something without iframe, i read about:

boxy: http://onehackoranother.com/projects/jquery/boxy/tests.html

simplemodal: http://www.ericmmartin.com/projects/simplemodal/

jqModal: http://dev.iceburg.net/jquery/jqModal/

@liho1eye : putting comments now

Edit: With help of @liho1eye i reach it:

<script type="text/javascript">
    //-------------------------------------------------
    var url_trg = '@Url.Content("~/Teste/opendialog")';
    var url_prl = '@Url.Content("~/Images/waitplease.gif")';
    //-------------------------------------------------
    function onloadpartial() {
        /*setupDialog("#opendialog", "#tempcontent", "section[id='main']", url_trg);*/
        configDetailDialog(url_trg, "#tempcontent", "section[id='main']", "Detail", "#opendialog");
    }
    //-------------------------------------------------
    function configDetailDialog(trgurl, containerselector, contentselector, dlgtitle, buttonselector) {
        //-------
        $(document).ajaxError(
            function (event, jqXHR, ajaxSettings, thrownError) {
                alert('[event:' + event + '], ' +
                        '[jqXHR:' + jqXHR + '], ' +
                        '[jqXHR_STATUS:' + jqXHR.status + '], ' + 
                        '[ajaxSettings:' + ajaxSettings + '], ' +
                        '[thrownError:' + thrownError + '])');
            });
        //-------
        $.ajaxSetup({ cache: false });
        //-------
        $(buttonselector).click(function (event) {
            event.preventDefault();
            openAjaxDialog(trgurl, containerselector, contentselector, dlgtitle);
        });
        //-------
    }
    //-------------------------------------------------
    function openAjaxDialog(trgurl, containerselector, contentselector, dlgtitle) {
        $.ajax({
            type: 'GET',
            url: trgurl,
            context: document.body,
            success: function (data) {
                var dlg = $(data).find(contentselector);
                $('#dlgdetail').remove();
                $(containerselector).append("<div id='dlgdetail'/>");
                $('#dlgdetail').append(dlg);
                $('#dlgdetail')
                    .css("border", "solid")
                    .dialog({
                        autoOpen: true,
                        modal: true,
                        title: dlgtitle,
                        open: function () {
                            configDetailDialog();
                        },
                        close: function (event, ui) {
                            $('#dlgdetail').remove();
                        }
                    })
                    .find("form").submit(function (event) {
                        alert('clicou ' + event);
                        var form = $(this);
                        var faction = "http://" + window.location.host + trgurl;
                        var fdata = form.serialize() + "&action:savedialog=savedialog";
                        $.ajax({                            
                            type: "POST",
                            url: faction,
                            data: fdata,
                            success: function (result) {
                                alert(result);
                            }
                        });
                        event.preventDefault();
                        $('#dlgdetail').dialog('close');
                    });
            }
        });
    }
    //-------------------------------------------------
</script>
-------------------------------------------------

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

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

发布评论

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

评论(1

用心笑 2024-12-28 03:22:00

老实说,我认为您的对话框创建代码确实过于复杂,存在不必要的 DOM 操作,而对话框创建会立即撤消这些操作。

不管怎样...我读到你的问题是“如何重写 AJAX 表单以通过 AJAX 提交?”。

嗯,很简单。在全局范围中添加此函数

var rewriteForms = function();
{
    $('#dlgdetail').find("form").submit(function()
    {
        var form = $(this);
        $('#dlgdetail').load(form.attr("action"), form.serializeArray(), rewriteForms);
        return false;
    });
}

然后在创建对话框后简单地调用

rewriteForms();

这将处理所有表单(假设它们是通过 而不是通过 js 代码)。如果有一些按钮或链接执行自定义操作,那么您需要以与上面所示相同的方式处理它们。

I honestly think your dialog creation code is really overcomplicated with unnecessary DOM manipulation, which is immediately undone by dialog creation.

Regardless... I read you question as "How do I rewrite AJAXed forms to submit via AJAX?".

Well, simple. Add this function in the global scope

var rewriteForms = function();
{
    $('#dlgdetail').find("form").submit(function()
    {
        var form = $(this);
        $('#dlgdetail').load(form.attr("action"), form.serializeArray(), rewriteForms);
        return false;
    });
}

Then simply call after dialog was created

rewriteForms();

This will take care of all forms (assuming they are submitted by <button type="submit">...</button> and not via js code). If there are some buttons or links that do something custom, then you need to handle them to in the same manner as shown above.

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