jQuery移动错误"无法在初始化之前调用listview上的方法"

21 listview jquery-mobile

我正在动态填充<ul data-role="listview">然后调用location.href="#Results"列表所在的位置listview('refresh').

所有这些都是在同一页面成功回调Ajax请求时完成的.它的工作或多或少,但我收到以下错误:

Uncaught cannot call methods on listview prior to initialization; attempted to call method 'refresh'
Run Code Online (Sandbox Code Playgroud)

我猜jQuery mobile还没有构建listview.我能做什么?

Arn*_*eil 40

只需先调用listview方法,不需要任何参数:

$('#myListview').listview().listview('refresh');
Run Code Online (Sandbox Code Playgroud)

解决方案取自http://www.gajotres.net/uncaught-error-cannot-call-methods-on-prior-to-initialization-attempted-to-call-method-refresh/

  • 哇!一个小时后,我的头靠在墙上,为我工作. (4认同)
  • 对我来说也一样:在JQM 1.4.5中,我尝试在[FullCalendar](http://fullcalendar.io/)中单击事件时创建一个动态弹出窗口。下面的代码`eventClick:function(calEvent,jsEvent,view){var $ popup = $('&lt;div data-role =“ popup” id =“ eventPopup”&gt; testPopup &lt;/ div&gt;')。appendTo('[data -role =“ content”]'); $('#eventPopup')。popup()。popup(“ open”); }` (2认同)

小智 28

您应该检查它是否已经初始化,刷新列表以防它初始化,否则触发按以下方式创建:

if ( $('#myListview').hasClass('ui-listview')) {
    $('#myListview').listview('refresh');
     } 
else {
    $('#myListview').trigger('create');
     }
Run Code Online (Sandbox Code Playgroud)


小智 14

我有同样的错误.我通过在查询中添加":visible"来解决它,因此只有在列表可见时才会运行.

所以你的代码看起来像这样:

$('#myListview:visible').listview('refresh');
Run Code Online (Sandbox Code Playgroud)

对我来说工作得很好!


Mar*_*ark 10

http://jquerymobile.com/demos/1.1.0/docs/api/events.html 你必须挂钩pageinit事件.在此之前,您无法调用任何JQM方法.即:

$('#Results').bind('pageinit', function() {
  $('#myListview').listview('refresh');
});
Run Code Online (Sandbox Code Playgroud)

  • 什么是'#results'在这里 (3认同)
  • @ user1656416 #Results是jQuery Mobile页面ID.更多信息:http://jquerymobile.com/demos/1.2.0/docs/pages/dialog/index.html. (2认同)