0

I'm using jQueryMobile to create an ipad application. I have the following code:

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");
                       }

                       }

                       });

            }

When I call the coursesandmodules function in the onDeviceRead() function and submit a login form that calls the submit method, the redirect works. But right now I'm hardcoding the companyDBName and studentID values used in the coursesandmodules method. I tried calling coursesandmodules() in the submit method and passing the values from there with something like:

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

And I removed the call to cursesandmodules() from the onDeviceRead() function. The redirect didnt work. It remained on the login page

4

1 回答 1

0

在您的代码中,您只是简单地指向一个锚点,文档对此只字未提:

文档

这些是他们的例子:

    //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
});
于 2012-06-06T16:04:36.183 回答