2014-01-18 67 views
0

我提前道歉,如果我是模糊的或者我的代碼是很難理解,我還在學習這些東西。我試圖顯示存儲在數組中的信息。我想要顯示這個信息,當一個按鈕被點擊時,當它再次被點擊時,陣列中的下一個索引顯示它的信息..去使用的onclick數組的下一個索引在Javascript

我需要幫助設置一個函數,以前進到數組的下一個索引。謝謝!

(函數(){

var students =[      //array of information 
    {name:'john', 
    address:{ 
     address:'821 Imaginary St', 
     city:'Chicago', 
     state:'Il'}, 
    gpa:[4.0,3.5,3.8]}, 

    {name:'jim', 
    address:{ 
     address:'127 fake Rd', 
     city:'Orlando', 
     state:'Fl'}, 
     gpa:[2.5,3.3,3.6]}]; 

    var redBut = document.querySelector('.buttonred'); 
redBut.onclick = getInfo; 
var count = 0; 

    function getInfo(){ 

     var stn = students[0]; 
     if(count<3){ 
     count++; 

     document.getElementById('name').innerHTML = 'Name: ' + stn.name;   //this is what is to be displayed when the button is clicked 
     document.getElementById('address').innerHTML = 'Address: ' + stn.address.address + " " + stn.address.city + ", " + stn.address.state; 
     document.getElementById('gpa').innerHTML = 'GPA: ' + stn.gpa[0] +", " + stn.gpa[1] + ", " + stn.gpa[2]; 
     document.getElementById('date').innerHTML = 'Date: ' + d.toLocaleDateString(); 
     document.getElementById('gpaavg').innerHTML = 'Average GPA: ' + gpas; 

}

}

回答

0

我想你想:var stn = students[count];

而不是:var stn = students[0];

(DOH)

+0

哇,真是那麼簡單!謝謝!! – Rich

相關問題