2013-06-03 116 views
4

我正在使用jquery validate.js验证我的jQuery移动应用程序中的联系表单。 当我通过url或页面刷新加载页面时,验证似乎完美。 但是,当我从另一页面链接返回到联系表单时,刷新则验证停止工作。jquery验证无法在jquery mobile中刷新页面不起作用

它在我刷新页面时起作用。 我试过以下内容:

1)包括jquery mobile js之前的contact.js。

2)data-ajax="false";

使用3)bind

$(document).bind("mobileinit", function(){ 
     $.extend( $.mobile , { ajaxEnabled: false }); 
}); 

4)refresh form page with jQuery Mobile

5)使用window.onload

window.onload = function() { 
    if(!window.location.hash) { 
    window.location = window.location + '#loaded'; 
    window.location.reload(); 
} 
} 

5)Jquery-Mobile: How to reload/refresh the internal page

6)One time page refresh after first page load Javascript working only on page refresh in jQuery mobile

但是这些都不起作用。当我导航到它时,我需要做的是刷新页面一次。 这是我的jQuery手机联系表格代码。

<form method="post" action="" id="feedback" name="feedback"> 
    <input type="text" name="user_name" data-role="none" id="name" value="" placeholder= "Your name*" /> 
    <br /> 
    <input class="email" required="true" type="text" name="user_email" data-role="none" id="email" value="" placeholder="Your Email*" /> 
    <br /> 
    <textarea cols="70" rows="12" class="required" name="user_message" id="c_message" data-role="none" placeholder="Your message*"> 
    </textarea> 
    <br /> 
    <input type="submit" value="Send" data-mini="true" data-role="none" data-inline="true" class="green_like" id="send" /> 
</form> 

这里是JS代码躺在“联系我们”的形式。

$(document).bind("pageinit",function(){ 

    $("#feedback").validate({ 

    submitHandler : function() { 
     $.ajax({ 
      type:'POST', 
      url :'/some/url', 
      data: $('#feedback').serialize(), 
      success: function(resp) { 
       if(resp==1) 
       { 
        $("#success_post_contact").html('<label style="color:green;margin-left: 6px;" >Thank you for contacting us.</label>'); 

       } 

      } 
     });//end of ajax 

    }//end of submit handler 
    });//end of validate 
});//end of document.bind 

回答

3

您没有尝试正确的事情。要了解这种情况,您需要了解jQuery Mobile如何工作。它使用ajax来加载其他页面。

第一页正常加载。它的HEAD和BODY被加载到DOM中,并且它们在那里等待其他内容。当第二页被加载时,只有它的BODY内容被加载到DOM中。而当我只说BODY内容时,我的意思是只有一个页面DIV(带有属性data-role =“page”)而没有别的。这个问题有几种解决方案,这里有一个list

+2

是的,那就是我做错了。现在我包括我的js文件在页面启动后,现在它的工作完美。谢谢你的答案。我花了3个小时处理这个问题,现在它解决了。 :) – Daenarys