2012-06-06 33 views
0

我正在使用jQueryMobile创建一个ipad应用程序。我有以下代码:jquery手机重定向未发生

function coursesandmodules() 
      { 
       var companyDBName = "abc3"; 
       var studentId = "2"; 
       $.ajax({ 
         type : "GET", 
         url : "http://192.168.1.78:8087/teach/rest/student/getcoursesandmodules/" + companyDBName + "/" + studentId , 
         dataType : "json", 
         error: function(error) { 
         alert("ERROR"); 
         }, 
         success : function(data,text,xhqr) { 

         var divElement = "<div id='coursesandmodules' data-role='page'><header data-role='header'><h1>Courses and Modules</h1></header><ul data-role='listview'></ul></div>"; 
         $('#loginpage').after(divElement); 
         var jsonobj = eval(data); 
         for (i in jsonobj) { 
         var stringArray = separate(i); 
         var CTId = stringArray[0]; 
         var CTName = stringArray[1]; 

         var listDivider = "<li data-role='divider' data-theme='b'>" + CTName + "</li>"; 
         $('#coursesandmodules ul').append(listDivider); 
         for(j in jsonobj[i]) { 
         var myObj = JSON.stringify(jsonobj[i][j]); 
         var myJsonObj = eval('(' + myObj + ')'); 
         for (h in myJsonObj) { 
         var moduleId = h; 
         var moduleName = myJsonObj[h]; 
         var list = "<li><a href='#module"+ moduleId + "'>" + moduleName + "</a></li>"; 
         $('#coursesandmodules ul').append(list); 
         topics(moduleId, moduleName, companyDBName); 
         } 
         } 
         } 
         } 
         }); 
      } 

      function topics(moduleId, moduleName, companyDBName) 
      { 
       var id = "module" + moduleId; 
       var url = "http://192.168.1.78:8087/teach/rest/student/getmoduletopics/" + companyDBName + "/" + moduleId; 
       $.ajax({ 
         type : "GET", 
         url : url, 
         dataType : "json", 
         error : function(error) { 
         alert("ERROR"); 
         }, 
         success : function(data,text,xhqr) { 
         var jsonobj = eval(data); 
         var modulePage = "<div id='" + id + "' data-role='page'><header data-role='header'><h1>" + moduleName + "</h1><a href='#coursesandmodules'>Back</a><a href='#' onclick='savemodule(" + moduleId + ")'>Save</a></header></div>"; 
         $('#loginpage').after(modulePage); 
         for (i in jsonobj){ 
         var stringArray = separate(i); 
         var topicId = stringArray[0]; 
         var topicName = stringArray[1]; 
         var topicText = jsonobj[i]; 

         var topicCollapsible = "<div data-role='collapsible'><h1>" + topicName + "</h1>" + topicText + "</div>"; 
         $("#" + id).append(topicCollapsible); 
         } 
         } 

         }); 

      } 

      function separate(string) { 
       var str = string.substring(1,(string.length - 1)); 
       var stringArray = str.split("="); 
       return stringArray; 
      } 


      function submit() { 

       var username = $("#username").val(); 
       var password = $("#password").val(); 

       var url = "http://192.168.1.78:8087/teach/rest/student/authenticate?username=" + username + "&password="+ password; 

       $.ajax({ 
         type : "GET", 
         url : url, 
         dataType : "json", 
         error : function(error) { 
         navigator.notification.alert("Error connecting to webservice"); 
         }, 
         success : function(data,text,xhqr) { 
         if(data) { 
         var jsonobj = eval(data); 

         // student info 
         var currentModule = data.currentModule; 
         var studentId = data.studentId; 
         var studentUserId = data.studentUserId; 
         var companyDbName = data.companyDbName; 
         var studentName = data.studentName; 
         var userName = data.userName; 
         var companyId = data.companyId; 
         //save data 
         $.mobile.changePage('#coursesandmodules'); 
         } 
         else { 
         navigator.notification.alert("Incorrect username/password combination"); 
         } 

         } 

         }); 

      } 

当我打电话的onDeviceRead()函数coursesandmodules功能,并提交调用提交方法的登录表单,重定向工作。但是现在,我正在硬编码在coursesandmodules方法中使用的companyDBName和studentID值。我试着打电话coursesandmodules()在提交方法,并从那里传递值的东西,如:

coursesandmodules(companyDBName, studentID) 
$.mobile.changePage('#coursesandmodules'); 

而且我删除从onDeviceRead()函数调用cursesandmodules()。重定向不起作用。它仍然在登录页面上

+0

呀,好。会做。 – jokham

回答

0

在你的代码很简单点锚,该文档提到一无所知:

docs

这些都是他们的例子:

//transition to the "about us" page with a slideup transition 
$.mobile.changePage("about/us.html", { transition: "slideup"}); 

//transition to the "search results" page, using data from a form with an id of "search" 
$.mobile.changePage("searchresults.php", { 
    type: "post", 
    data: $("form#search").serialize() 
}); 

//transition to the "confirm" page with a "pop" transition without tracking it in history 
$.mobile.changePage("../alerts/confirm.html", { 
    transition: "pop", 
    reverse: false, 
    changeHash: false 
}); 
+0

您可以使用锚点。我有一个名为anchor和data-role ='page'的id的div。使用重定向上面的代码工作。我的猜测是它不工作,因为coursesandmodules div还没有创建。注意第一种方法中有些元素正在添加到dom中。所以当我先用onDeviceReady调用这个方法时,然后提交表单,重定向就起作用了。如果我从submit()调用它并从onDeviveReady中删除它,它不起作用 – jokham