简体   繁体   English

qTip2中的Ajax形式

[英]Ajax form in qTip2

I have a table with a list of names, their attributes and comments for each record. 我有一个表,其中列出了名称,它们的属性和每条记录的注释。 I want to be able to display the comments in a tooltip, and also be able to update those comments via Ajax. 我希望能够在工具提示中显示评论,也可以通过Ajax更新这些评论。 I would like to show a tooltip or a modal by clicking on a link. 我想通过单击链接来显示工具提示或模式。 This modal will have a textarea with the comments preloaded. 该模式将具有一个文本区域,其中预载了注释。 The user can modify the comments and submit them to the action page via Ajax. 用户可以修改评论,然后通过Ajax将其提交到操作页面。 On successful submission the existing tooltip content will also need to be updated. 成功提交后,现有的工具提示内容也将需要更新。

Any help would be greatly appreciated. 任何帮助将不胜感激。

I am using the qtip2 and tipsy plugins. 我正在使用qtip2和tipy插件。

I am loading the form in the qTip2 tooltip, onclick, through ajax. 我正在通过ajax在qTip2工具提示onclick中加载表单。 The link to the form is brought over from the rel tag. 表单的链接是从rel标记移过来的。 Now when I submit the form, it doesn't submit through ajax but directly the action page. 现在,当我提交表单时,它不是通过ajax提交的,而是直接通过操作页面提交的。 This is my JS code: 这是我的JS代码:

    $('.commentsedit').each(function()
        {
            // We make use of the .each() loop to gain access to each element via the "this" keyword...
            $(this).qtip(
            {
                content: {
                    // Set the text to an image HTML string with the correct src URL to the loading image you want to use
                    text: '<img class="throbber" src="images/throbber.gif" alt="Loading..." />',
                    ajax: {
                        url: $(this).attr('rel') // Use the rel attribute of each element for the url to load
                    },
                    title: {
                        text: $(this).attr('title'), // Give the tooltip a title using each elements text
                        button: true
                    }
                },
                position: {
                    at: 'bottom center', // Position the tooltip above the link
                    my: 'top right',
                    viewport: $(window), // Keep the tooltip on-screen at all times
                    effect: false // Disable positioning animation
                },
                show: {
                    event: 'click',
                    solo: true // Only show one tooltip at a time
                },
                hide: 'unfocus',            
                style: {
                    classes: 'my_width_setting_class qtip-wiki qtip-light qtip-shadow'
                },
                events: {
                    render: function(event, api) {
                        // Capture the form submission
                        $('form', this).bind('submit', function(event) {
                            // Grab and store input elements
                            var inputs = $(':textarea', this);

                            // Common ajax error handler
                            function errorHandler(jqXHR, message) {
                                // Set the error and show/hide it
                                $('.error', api.elements.tooltip).html(message || '').toggle(!!message);
                            }

                            // Setup AJAX request
                            $.ajax({
                                url: 'commentsform.cfm',
                                data: $(this).serialize(),
                                type: 'post',
                                dataType: 'json',
                                success: function(data, status, jqXHR) {
                                    // On success, show message and refresh after 2 seconds
                                    if(data.status === 'success'){
                                        api.set('content.text', data.message + ' Redirecting...');
                                        setTimeout(function(){ window.location.reload() }, 2000);
                                    }

                                    // Call error handler on error status too.
                                    else { errorHandler(jqXHR, data.message); }
                                },
                                error: errorHandler,

                                // Disable/Enable input elements
                                beforeSend: function() { inputs.attr('disabled', 'disabled'); },
                                complete: function() { inputs.removeAttr('disabled'); inputs[0].focus(); }
                            });

                            // Prevent normal form submission
                            event.preventDefault();
                        });
                    }
                }
            })
        })

Although an old question, I think that someone will find useful the solution proposed to a similar problem in the qtip2 developer's site and specifically in 尽管这是一个古老的问题,但我认为有人会在qtip2开发人员的网站(特别是在
http://craigsworks.com/projects/forums/showthread.php?tid=3680 http://craigsworks.com/projects/forums/showthread.php?tid=3680

Edit: in response to a comment I reproduce the main part of the answer as a reference: 编辑:在评论时,我将答案的主要部分作为参考:

$('a[class=qTipForm][rel]').each(function(){
    var formName = $(this).attr('name');

    $(this).qtip({
        content: {
            //text: '<iframe src="'+$(this).attr('rel')+'" height="400px" width="700px" frameborder="0"></iframe>',
            text: 'Loading...',
            ajax: {
                url: $(this).attr('rel'),
                success: function(data) {
                    // Set the tooltip contents
                    this.set('content.text', data);

                    // Bind the form submit event
                    $('#' + formName).bind('submit', function(event) {
                        // Grab and store input elements
                        var inputs = $(':input','#' + formName);

                        // Common ajax error handler
                        function errorHandler(jqXHR, message) {
                            // Set the error and show/hide it
                            $('.error', api.elements.tooltip).html(message || '').toggle(!!message);
                        }

                        // Setup AJAX request
                        $.ajax({
                            url: $('#' + formName).attr('action'),
                            data: $('#' + formName).serialize(),
                            type: 'post',
                            dataType: 'json',
                            success: function(data, status, jqXHR) {
                                // On success, show message and refresh after 2 seconds
                                if(data.status === 'success'){
                                    api.set('content.text', ' Redirecting...');
                                    setTimeout(function(){ window.location.reload() }, 2000);
                                }

                                // Call error handler on error status too.
                                else { errorHandler(jqXHR, data.message); }
                            },
                            error: errorHandler,

                            // Disable/Enable input elements
                            beforeSend: function() { inputs.attr('disabled', 'disabled'); },
                            complete: function() { inputs.removeAttr('disabled'); inputs[0].focus(); }
                        });

                        // Prevent normal form submission
                        event.preventDefault();
                    })
                }
            },
            title: {
                text: $(this).attr('title'),
                button: true
            }
        },
        position: {
            my: 'center',
            at: 'center', // Position the tooltip above the link
            target:$(window),
            effect: false // Disable positioning animation
        },
        show: {
            event: 'click',
            solo: true, // Only show one tooltip at a time
            modal: true
        },
        hide: false,
        style: {
            classes: 'viewTipForm ui-tooltip-rounded ui-tooltip-light',
            tip: false
        }
    })

    .click(function(event) { event.preventDefault(); });
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM