2014-10-29 38 views
0

如何讓我的選擇列表中的每個單詞都鏈接到另一個頁面?使選擇列表中的每個選項都鏈接到不同頁面

leaderboard選擇動態填充選項(根據下面的代碼)時,我想讓選項可點擊並讓它們重定向到指定的頁面。

<select id='standings' name='standings' onchange="listTeam(this)"> 
    <option value='0'>A</option> 
    <option value='1'>B</option> 
    <option value='2'>C</option> 
    <option value='3'>D</option> 
</select> 

<select id='leaderBoard' name='leaderBoard' multiple="multiple" size="1" style="width: 100px;"> </select> 

<script type="text/javascript"> 

    var teams = [ 
       "x y z ", 
       "e r t z u ", 
       "w e r t", 
      ], 

      listTeam = function listTeam(sel) { 
       var val = document.getElementById('standings').value, //get the selected value 
         team = teams[val], //get the selected team, based on value 
         lb = document.getElementById('leaderBoard'); //get the leaderBoard select element 

       lb.options.length = 0; 

       var people = team.trim().split(/\s/); 
       for (var j = 0; j < people.length; j++) { 
        var opt = document.createElement('option') 
        opt.innerText = people[j]; 
        lb.appendChild(opt); 
       } 
      }; 
    listTeam(); 

</script> 
+1

'select's不能包含錨點(超鏈接)。你究竟在努力完成什麼? – David 2014-10-29 13:10:16

+1

「我怎麼能從我的選擇中找到每一個字」我無意中想到了你一個字。 – 2014-10-29 13:12:41

+0

var teams = [「x y z」, 「e r t z u」,「w e r t」,......我想要例如「x」是超鏈接到另一頁 – user200 2014-10-29 13:14:08

回答

0

與甲測試只需要改變陣列等,其餘工作,設置的內部網址「值=」上<option>,關於篩選重定向窗口位置

var teams = [ 
     "x y z ", 
     "e r t z u ", 
     "w e r t", ]; 

    var urls = ["www.a.com", "www.a1.com", "www.a2.com"]; 

    listTeam = function listTeam(sel) { 
     var val = document.getElementById('standings').value, //get the selected value 
      team = teams[val], //get the selected team, based on value 
      lb = document.getElementById('leaderBoard'); //get the leaderBoard select 
     lb.options.length = 0; 

     var people = team.trim().split(/\s/); 

     for (var j = 0; j < people.length; j++) { 
      var opt = document.createElement('option') 
      opt.value = urls[j]; 
      opt.innerText = people[j]; 
      lb.appendChild(opt); 
     } 

     lb.onchange = function() { 
      console.log("change"); 
      if (this.selectedIndex) { 
       window.location.href = this.value; 
      } 
     } 
    }; 
    listTeam(); 

HTML

<select id='standings' name='standings' onchange="listTeam(this)"> 
    <option value='0'>A</option> 
    <option value='1'>B</option> 
    <option value='2'>C</option> 
    <option value='3'>D</option> 
</select> 
<select id='leaderBoard' name='leaderBoard' multiple="multiple" size="1" style="width: 100px;"> 
相關問題