2013-06-03 90 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