2012-07-19 138 views
0

我有一個列出課程的頁面。當學生點擊其中一個課程標題時,它將使用AJAX將參數(courseId)傳遞到第二個頁面,該頁面將返回課程詳細信息。如何在第二頁訪問參數(courseId)?如果我直接在源而不是訪問參數放一個值,它顯示的細節:如何從第二頁訪問參數?

var idInput = 12345; 
var regNoInput = 098766; 

當我試圖getElementById訪問的參數,它並沒有表現出任何的細節:

var idInput = document.getElementById("courseId").value; 
var regNoInput = document.getElementById("regNo").value; 

我也試過訪問參數作爲PHP變量,但仍然沒有細節:

var idInput = "${courseId}"; 
var regNoInput = "${regNo}"; 

這是我的第一個網頁腳本,參數是通過使用URL:

success: function(json_results){ 
    $('#list').append('<ul data-role="listview" data-inset="true"</ul>'); 
    listItems = $('#list').find('ul'); 
    $.each(json_results.rows, function(key) { 
    html = "<li data-mini='true' id='icon'><a href='MRegisteredClassesDetail.html? 
      courseId=" + [json_results.rows[key].courseId] + "&regNo=" + 
      [json_results.rows[key].regNo] +"' rel='external'>" + 
      json_results.rows[key].courseName+ "</a>" + "<a 
      href='http://137.57.102.146:8080/Training/MRateCourse.phone?courseId="+ 
      [json_results.rows[key].courseId] + "&regNo=" + 
      [json_results.rows[key].regNo] + "' rel='external'>RATE THIS COURSE</a> 
      </li>" ; 
    listItems.append(html); 
    }); 

,這是我的第二頁,它應該讀取參數值:

var idInput = "${courseId}"; 
    var regNoInput = "${regNo}"; 

    success: function(json_results){ 
    $('#list').append('<ul data-role="listview" data-inset="true"</ul>'); 
    listItems = $('#list').find('ul'); 
    $.each(json_results.rows, function(courseId) { 
    html ='<h1 align=center>'+json_results.rows[courseId].courseName+'</h1>'; 
    html +='<li>Registration Number:'+json_results.rows[courseId].regNo+'</li>'; 
    html +='<li>Registration Date:'+json_results.rows[courseId].regDate+'</li>'; 
     listItems.append(html); 
    }); 
+0

在url中做參數? – artwl 2012-07-19 01:41:59

+0

你如何將信息傳遞到下一頁? – atomSmasher 2012-07-19 01:50:21

+0

yup ..它在url ..這裏的網址.. " + json_results.rows[key].courseName+ " sone 2012-07-19 02:05:46

回答

0

我聲明一個變量

var idInput = getParameterByName('courseId'); 

並定義功能

function getParameterByName(name) { 
     var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search); 
     return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); 
    } 

它運作良好。

0

如果URL參數,試試這個:

var params=window.location.search.substr(1).split("&"), 
    idInput=params[0].split("=")[1], 
    regNoInput=params[1].split("=")[1]; 
+0

nope ...我無法得到它.. :( – sone 2012-07-19 06:29:16