AJAX之后的jQuery移动刷新元素

AJAX之后的jQuery移动刷新元素,ajax,jquery-mobile,refresh,Ajax,Jquery Mobile,Refresh,我正在使用jqm1.4.3。我有一个AJAX,它设置localStorage键的值,然后更改页面: login.js $.ajax({ url: url, type: 'GET', dataType: 'json', data: 'method=login&username=' + encodeURIComponent(username) + '&token=' + encodeURIComponent(token), //async: f

我正在使用jqm1.4.3。我有一个AJAX,它设置localStorage键的值,然后更改页面:

login.js

$.ajax({
    url: url,
    type: 'GET',
    dataType: 'json',
    data: 'method=login&username=' + encodeURIComponent(username) + '&token=' + encodeURIComponent(token),
    //async: false,
    //crossDomain: true,
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log(JSON.stringify(XMLHttpRequest));
        console.log(JSON.stringify(textStatus));
        console.log(JSON.stringify(errorThrown));
    },
    success: function( user ){
        console.log(JSON.stringify(user));
        if(user) {
            $('#welcome').remove();
            storage.setItem("uid", user);
            storage.setItem("username", username);
            $('#welcome').trigger("create");
            $.mobile.changePage( "#page_dashboard", { transition: "slideup" });
            console.log(storage.username);
        }
    }
});
index.html

<div data-role="content">
     <h2 align="center" id="welcome">Hello</h2>
</div>
$( document ).delegate("#page_dashboard", "pageinit", function() {
        $('#welcome').trigger("create");
        $('#welcome').text("Hello " + storage.username);
});
我试图刷新#page_仪表板上文本的值以反映新用户。我正在使用page_init和page_beforecreate,两者都不刷新文本。如果我手动刷新页面,文本将更改。如何刷新元素以反映新的本地存储值?

  • “pageinit”DOM就绪时触发。如果你想做的话 然后只使用一次pageinit

  • $( document ).delegate("#page_dashboard", "pagebeforeshow", function() {
       $('#welcome').trigger("create");
       $('#welcome').text("Hello " + storage.username);
    });
    
  • “pagebeforeshow”在页面显示之前触发。你 当需要在显示前执行操作时,可以使用此事件, 比如adddiv或HTML结构如果每次打开页面时都需要它 已访问,然后使用pagebeforeshow

    $( document ).delegate("#page_dashboard", "pagebeforeshow", function() {
       $('#welcome').trigger("create");
       $('#welcome').text("Hello " + storage.username);
    });
    

如果您使用的是JQM 1.3或更低版本,您需要
$.mobile.activePage.trigger(“pagecreate”)
。然后
$(“#headerID”).toolbar()
。它不是JQM标题…它只是html标题标记中页面包装上的文本。请使用html标记和任何相关数据更新您的问题。如果它只是文本,那么
$(“#欢迎”)。文本(“新建文本”)
而不是
.remove()
。我在login.js中删除了对#welcome的所有引用,只设置了localStorage,然后在pagebeforeshow中执行了上述操作。谢谢!