1

首先,我不确定哪个是正确的小部件,弹出窗口或对话框。我现在要弹出窗口。我有一个表单,提交后我想在弹出窗口中显示结果。验证和 JSON 的东西在这里:

$(function(){
    $.fn.serializeObject = function() {
        //serializeObject function stuff
    };

    $(document).on('click', function() {
        $('#myForm').validate({
            errorPlacement: function (error, element) {
                if (element.is('select')) {
                    error.insertAfter(element.parents('div.ui-select'));
                } else {
                    error.insertAfter(element.parents('div.ui-input-text'));  // default placement
                    }
                },
                    errorElement: 'em',
                    submitHandler: function(form){
                    var wrapper = {};
                    var location = {};
                    wrapper.location = $("#newLocation").serializeObject();
                    $.ajax({
                        type: $(form).attr('method'),
                        url: 'server' + 'service' + '/' + 'servicecall',
                        dataType: 'json',
                        async: false,
                        data: JSON.stringify(wrapper),
                        clearform: true,
//****Here is where the fun starts**
                        success: function(msg) {
                                var ID = msg.location.id;
                                //other element ID's...
                                //pop up vars
                                var $popUp = $("<div/>").popup({
                                    dismissable: true,
                                    theme: "d",
                                    overlayTheme: "a",
                                    transition: "pop"
                                }).on("popupafterclose", function () {
                                    $(this).remove;
                                });
                                if(msg.success){
                                    $('#newLocation').slideUp('fast');
                                    $popUp.popup('open').append(ID);
                                 }
                            },
                            error: function(msg) {
                                $popUp.popup('open').append('Error');
                            }
                        });
                        return false;
                    },
                    rules: {
                        //rules
                    },
                    messages: {
                       //messages
                    }
                });
            });

我需要动态创建弹出窗口并附加一些返回的数据。我可以将返回的数据附加到成功时显示的隐藏 div/table 中,但想尝试使用动态创建的弹出窗口(或最好的对话框)来尝试此操作。我只是无法让它创建弹出窗口。有任何想法吗?

4

1 回答 1

1

您必须在 ajax 部分之外创建弹出窗口,以便所有段(成功、错误等)都可以使用它

popup = '<div data-role="popup" id="popup" data-theme="d" data-overlay-theme="a" data-dismissable="true" data-transition="pop"></div>';

$.mobile.activePage.append( popup ).trigger( "pagecreate" );

$( document ).on( "popupafterclose", ".ui-popup", function() {
    $( this ).remove();
});

然后你就可以使用它了

success: function(msg) {
     var ID = msg.location.id;
     //other element ID's...
     if(msg.success){
        $("#popup").popup('open').append(ID);
     }
},
error: function(msg) {
      $("#popup").popup('open').append('Error');
}

我相信这样的事情会奏效。

UPDATE 1 I took the above from the documents. Yours could work too, but you don't attach it in the DOM. Yes you create an HTML element (<div/>) but you don't attach it to the DOM. Maybe attaching it to active page as above could fix yours.

UPDATE 2 I tried to fix some other faults, like the one that you use the popup in the error: but you create the popup only in success:

于 2013-06-19T05:22:39.297 回答