jquery移动重定向没有发生

时间:2012-06-06 15:59:40

标签: jquery-mobile

我正在使用jQueryMobile来创建ipad应用程序。我有以下代码:

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函数并提交一个调用submit方法的登录表单时,重定向工作。但是现在我正在硬编码coursesandmodules方法中使用的companyDBName和studentID值。我尝试在submit方法中调用coursesandmodules()并从那里传递值:

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

我从onDeviceRead()函数中删除了对cursesandmodules()的调用。重定向不起作用。它保留在登录页面上

1 个答案:

答案 0 :(得分: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
});