1

我想在加载页面之后触发一个更改页面。下面是我使用的代码。提交后,将在外部站点(使用 loadPage)触发数据库操作。这样可行。接下来,我想切换到另一个发布了 guid(唯一 ID)变量的页面。

我无法让这个工作。希望有人可以。提前致谢。

        $(document).on('pageinit', '#page1', function(){

    $('form').submit(function(){

        var guid = GUID();
        $.mobile.loadPage( "http://domain.com/dbaction.php?guid="+guid, {
        type: "post", 
        data: $("form#addtegel").serialize()

        });     
        return false; 
    $.mobile.changePage ($("#page2"),{ transition: "slideup"} );

     });


   });

$(document).on('pageinit', '#page2', function(){
DoSomething(guid);
});
4

1 回答 1

0

首先,您需要使 guid 成为全局变量。目前,它在您的表单提交中是本地的,因此无法在该表单提交操作之外访问它。

其次,不要使用- 这是顺便loadPage()调用的 jQuery Mobile 的内部函数-像这样使用:changePage()$.ajax()

var guid;
$(document).on('pageinit', '#page1', function(){

    $('form').submit(function(){

        window.guid = GUID();
        $.ajax({
            url: "http://domain.com/dbaction.php?guid="+guid,
            type: "post", 
            data: $("form#addtegel").serialize(),
            success: function() {
                $.mobile.changePage ($("#page2"), { transition: "slideup"} );
            }
        });
        return false; 
     });

   });

$(document).on('pageinit', '#page2', function(){
    DoSomething(window.guid);
});

另外,请注意混合使用 GET 和 POST 数据是一个非常糟糕的习惯。我说的是这个网址:"http://domain.com/dbaction.php?guid="+guid

There should only be GET or POST data present in a single request, not both. Would it not be possible to pass guid to dbaction.php in the POST (using a hidden field in your form)?

于 2012-10-06T20:34:01.243 回答