2013-05-01 82 views
0

我創建了一個phonegap android應用程序,裏面有很少的HTML頁面,並使用jquery ajax在服務器之間傳輸數據。 要在頁面之間移動我正在使用jquery $ .mobile.changePage()方法。但是當我從索引頁面(最初加載的URL)移動到另一個頁面的頁面後,該頁面中的jquery方法無法工作。在HTMl頁面之間移動電話

但是,當我最初使用java代碼加載特定頁面,

super.loadUrl("file:///android_asset/www/secondpage.html");它的功能很好。

我試着用, $.mobile.changePage("secondpage.html", { transition: "flip" });還有,

$.mobile.changePage("file:///android_asset/www/secondpage.html", { transition: "flip" }); 

既不爲我工作。第一頁

$(document).ready(function(){  
$('#submitbtn').click(function(){  
$vari = $('#signin').serialize(); 
alert($vari); 
$.mobile.loading("show"); 
$.get('http://192.**.**.**/~114178R/v_login/signin.php', $vari, 

      function(data){ 
       $('#result').html(data); 
       $.mobile.loading("hide"); 
      });    


      return false; 

}); 

//above part works fine. 
//Code to move to the second page. and it also moves. 
$('#signup').click(function(){ 
$.mobile.changePage("second.html", { transition: "flip" }); 

}); 

}); 

代碼我重視的js代碼的第二頁,並在second.html的頭宣佈它。但在第二頁JavaScript沒有調用。但只是css文件正在加載。在本地主機上這個頁面和JavaScript的功能很好。但在模擬器和設備問題發生。當我按提交按鈕(部署後)它只刷新窗體。但是當我從java文件加載second.html作爲索引頁面時,它運行良好。

super.loadUrl("file:///android_asset/www/login.html"); // second page not works. 

super.loadUrl("file:///android_asset/www/second.html"); //second.html works well. 

HTML的second.html

<head> 
<!--<meta content="text/html; charset=utf-8" http-equiv="Content-Type">--> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<title>Register</title> 
<script type="text/javascript" src="assets/jquery 1.8.js"></script> 
<script type="text/javascript" src="assets/jquerymobile.js"></script> 
<script type="text/javascript" src="js/register.js"></script> 
<link rel="stylesheet" href="assets/jquerymobile.css"></link> 


</head> 

<body> 
<script type="text/javascript" src="js/register.js"></script> 
<div data-role="page" data-theme="b" id="maincont" data-add-back-btn="true"> 
     <div data-role="header"> 
     <h1>Register</h1> 
     <a href="login.html" data-rel="back"></a> 
     </div><!-- /header --> 

     <div data-role="content" id="form"> 
     <form id="signup" method="post"> 

     <label for="basic">Your Name:</label> 
     <input type="text" name="urname" id="urname" data-mini="true" /> 

     <br> 

     <label for="basic">User Name</label> 
     <input type="text" name="uname" id="uname" data-mini="true" /> 
     <br> 


     <br> 
     <li data-role="fieldcontain"> 
       <label for="select-choice-a" class="select">Choose your position :</label> 
       <select name="select-choice-a" id="select-choice-a" data-native-menu="false" data-theme="c"> 
        <option>New Team Member</option> 
        <option value="standard">Software Developer</option> 
        <option value="rush">Managerial Level</option> 
        <option value="express">Techlead</option> 
        <option value="overnight">Branch Manager</option> 
       </select> 
      </li> 
     <label for="basic">Password :</label> 
     <input type="password" placeholder="A-Z and a-z and 0-9" name="pw_1" id="pwi_1" data-mini="true"/> 
     <br> 
     <label for="basic">Enter password again :</label> 
     <input type="password" placeholder="A-Z and a-z and 0-9" name="pw_2" id="pwi_2" data-mini="true"/> 
     <br> 



     <input type="submit" id="submitbtn" value="Register Me" data-corners="true" data-shadow="true" data-iconshadow="true"> 
     <br> 

     </form> 
     <div id="result"></div> 
     </div><!-- /content --> 
     <br> 
     <br> 
     <div data-role="footer"> 
     <h4>Sign in to enjoy Rides!</h4> 
     </div><!-- /header --> 

    </div> 
</body> 

</html> 

的JavaScript register.js爲second.html

$(document).ready(function() { 

    $('#submitbtn').click(function() { 
    alert("clicked"); 

    if(($('#pwi_1').val()!= "") && ($('#pwi_1').val()==$('#pwi_2').val())){ 
     alert("if"); 
     alert($('#pwi_1').val()); 

     $vari = $('#signup').serialize(); 
     alert($vari); 
     $.mobile.loading("show"); 
     $.get('http://xxx.xxx.xxx.xxx/register.php', $vari, 

      function(data){ 
       $('#result').html(data); 
       $.mobile.loading("hide"); 
      });    

     return false; 
    } 
    else { 
     alert("else"); 
     alert($('#pwi_2').val()); 
    } 
}); 
}); 
+0

你在第二頁有一些javascript代碼嗎? – 2013-05-01 05:19:13

+0

是的,jQuery的方法來驗證和提交表單在那裏。 – Marlio 2013-05-01 05:29:38

+0

我認爲它失敗了,爲了檢查禁用這個代碼,並嘗試打開頁面 – 2013-05-01 05:51:00

回答

1

也許你可以使用window.location.replace(登錄。 HTML);

而當您使用Jquery時,請務必在您的所有頁面中導入腳本。

希望它幫助;)

+0

我剛纔發現,jquerymobile只能通過index.html的頭部。因此,我需要使用$ .on()方法將第二頁中的事件綁定到第一頁。但沒有任何想法如何去做,它會指導我在單個js文件中編寫項目的整個js。那麼對此有什麼幫助..?我找到了這個。 http://stackoverflow.com/questions/14667420/javascript-not-working-in-second-html-file-in-phonegap-jquery-mobile?rq=1和 http://stackoverflow.com/questions/14468659/jQuery的移動文檔準備VS-頁面事件 – Marlio 2013-05-01 08:34:56

0
<script type="text/javascript" src="js/register.js"></script> 

把上面的代碼你的頁面中。

<div data-role="page" data-theme="b" id="maincont" data-add-back-btn="true"> 

<div data-role="page" data-theme="b" id="maincont" data-add-back-btn="true"> 
<script type="text/javascript" src="js/register.js"></script> 

腳本沒有在本節只在一個頁面refesh運行。