2011-09-23 64 views
1

我已经与jQuery Mobile的一个奇怪的问题......下面是我的应用程序的HTML 代码jQuery Mobile的:通过ID不能很好地工作

<!-- == START MAIN PAGE == --> 
<div id="mainPage" data-role="page"> 
    <div data-role="header" data-position="fixed"> 
     <a href="#addContactPage" 
       data-transition="slidedown" 
       data-rel="dialog" 
       data-icon="plus" 
       data-iconpos="notext">Add a contact</a> 
     <h1>My repertory</h1> 
    </div> 
    <div data-role="content"> 
     <p> 
      Welcome to your repertory! 
     </p> 
     <ul id="contacts_list"></ul> 
    </div> 
    <div data-role="footer" data-position="fixed"> 
     <h1>Made with <em>jQuery Mobile</em></h1> 
    </div> 
</div> 
<!-- == END MAIN PAGE == --> 

<!-- == START ADD CONTACT PAGE == --> 
<div id="addContactPage" data-role="page"> 
    <div data-role="header"> 
     <h1>My repertory: add a contact</h1> 
    </div> 
    <div data-role="content"> 
     <form id="addContactForm" action="#mainPage"> 
      <div data-role="fieldcontain">  
       <input type="submit" value="Valid" /> 
      </div> 
     </form> 
    </div> 
</div> 

当主页显示,我想设置<UL>清单内的项目:

//When the main page is created. 
$('#mainPage').live('pagecreate',function() 
{ 
    //Just after the page is shown 
    $(this).bind('pageshow',function() 
    { 
     alert('Event triggered!'); 
     $('#contacts_list').html('<li>Reset list!</li>'); 
    }); 
}); 

所以,这里是我的问题:当我开始应用程序,此代码工作并设置项目。

如果我打开“添加联系人页面”(请注意它是一个对话框)并关闭它,此代码也可以正常工作,并且预期结果已完成。

但是,如果我打开“添加联系人页面”,然后提交它的形式,列表变为空! alert()已完成,因此事件被触发...但是该列表为空,就像没有调用方法那样(html())。 我试过提醒* $('#contacts_list')的值,长度为1,因此存在jQuery对象!

请注意,我在提交表单时没有做任何事情:提交事件没有听众。

如果我通过这一个替换代码:

//When the main page is created. 
$('#mainPage').live('pagecreate',function() 
{ 
    //Just after the page is shown 
    $(this).bind('pageshow',function() 
    { 
     alert('Event triggered!'); 
     $(this).find('#contacts_list').html('<li>Reset list!</li>'); 
    }); 
}); 

然后它工作得很好!

我真的不明白......

为什么预期的结果与$(本).find( '#contacts_list')来完成,而不是用$( '#contacts_list')?

在此先感谢!

+0

只是一个想法,如果对象存在,也许你需要刷新页面。尝试如下所示:$(this).find('#contacts_list')。html('

  • 重置列表!
  • ').trigger('create');或$(this).find('#contacts_list')。html('
  • 重设清单!
  • ').page('refresh'); –

    +0

    谢谢,但它没有工作:第一个代码不会改变,而第二个代码会触发一个错误:_cannot在初始化之前在页面上调用方法;试图调用方法'refresh'_。我提醒说'$(this).find('#contacts_list')。html('

  • 重置列表!'
  • ');'效果很好,它的代码不起作用:'$('#contacts_list') .html('
  • 重置清单!
  • ');'。就好像两个** jQuery **对象不引用相同的DOM元素! – KorHosik

    回答

    0

    看起来您的表单提交导致第二个事件绑定到pageshow。如果您删除(或更改)表单上的操作会发生什么?另外,尝试:

    $(this).unbind('pageshow').bind('pageshow',function()...

    编辑

    我认为外部函数很可能是多余的。为什么不简化一大堆:

    $('#mainPage').live('pageshow',function() 
        { 
        alert('Event triggered!'); 
        $(this).find('#contacts_list').html('<li>Reset list!</li>'); 
        } 
    ); 
    
    +0

    当我从窗体中移除Action时会发生同样的问题。但是如果我在绑定之前尝试解除_pageshow_事​​件,会发生一种奇怪的行为:第一次提交表单时,列表仍为空,但第二次按预期工作......我不明白为什么!此外,每当页面显示时,就像_pagecreate_被激怒:我认为它只是在创建页面后才被激怒......!感谢您的回答:) – KorHosik

    +0

    在这种情况下,这听起来像'活'evnt被重复。 ('pagecreate')。live('pagecreate',...' – graphicdivine

    +0

    与$(this).unbind('pageshow')。bind('pageshow' ',function()...'出现这种行为是来自** jQuery Mobile **还是其他错误? – KorHosik

    相关问题