2015-07-01 66 views
-1

我是一個解決javascript的問題,我需要諮詢有關我的項目的建議。我有一個名爲主題和級別的輸入,在水平我想補充通過逗號分隔的多個值,在彈出的輸入框中添加多個值setAttribute

我創造了這個功能

function selectedLvl($levels){ 
    lvl1.setAttribute("value", $levels); 
    } 

在我彈出我有這樣的代碼,其中用戶點擊它並更新輸入。目前它的工作只有一個價值。我如何我添加逗號seprated多個按鍵,例如,如果他們clcik每個vallue它會像追加各級幼兒園,小學等..

 <ul> 
     <li><a href="#" onclick="selectedLvl('All Levels')">All Levels</a></li> 
     <li><a href="#" onclick="selectedLvl('Kindergarten')">Kindergarten</a></li> 
     <li><a href="#" onclick="selectedLvl('Primary School')">Primary School</a></li> 
     <li><a href="#" onclick="selectedLvl('School Years 7,8,9')">School Years 7,8,9</a></li> 
     <li><a href="#" onclick="selectedLvl('GCSE – Middle School')">GCSE – Middle School</a></li> 
     <li><a href="#" onclick="selectedLvl('HKDSE')">HKDSE</a></li> 
     <li><a href="#" onclick="selectedLvl('IB, AS/A, Senior Secondary')">IB, AS/A, Senior Secondary</a></li> 
     <li><a href="#" onclick="selectedLvl('Tertiary Level')">Tertiary Level 
     <li><a href="#" onclick="selectedLvl('Test Preparation')">Test Preparation</a></li> 
     <li><a href="#" onclick="selectedLvl('Computer/IT')">Computer/IT</a></li> 
     <li><a href="#" onclick="selectedLvl('Language Classes')">Language Classes</a></li> 
     <li><a href="#" onclick="selectedLvl('Business Training')">Business Training</a></li> 
     </ul> 

回答

1

相信你想LVL1是一個數組,或值爲數組的對象。

隨着LVL1作爲數組:

var lvl1=[]; 
 
function selectedLvl($levels){ 
 
    lvl1.push($levels); 
 
    console.log(lvl1); 
 
} 
 
    
<ul> 
 
    <li><a href="#" onclick="selectedLvl('All Levels')">All Levels</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Kindergarten')">Kindergarten</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Primary School')">Primary School</a></li> 
 
    <li><a href="#" onclick="selectedLvl('School Years 7,8,9')">School Years 7,8,9</a></li> 
 
    <li><a href="#" onclick="selectedLvl('GCSE – Middle School')">GCSE – Middle School</a></li> 
 
    <li><a href="#" onclick="selectedLvl('HKDSE')">HKDSE</a></li> 
 
    <li><a href="#" onclick="selectedLvl('IB, AS/A, Senior Secondary')">IB, AS/A, Senior Secondary</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Tertiary Level')">Tertiary Level 
 
    <li><a href="#" onclick="selectedLvl('Test Preparation')">Test Preparation</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Computer/IT')">Computer/IT</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Language Classes')">Language Classes</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Business Training')">Business Training</a></li> 
 
</ul>

隨着LVL1作爲一個對象:

var lvl1={}; 
 
lvl1.values=[]; 
 
function selectedLvl($levels){ 
 
    var temp=lvl1.values; 
 
    temp.push($levels); 
 
    lvl1.values=temp; 
 
    console.log(JSON.stringify(lvl1)); 
 
} 
 
    
<ul> 
 
    <li><a href="#" onclick="selectedLvl('All Levels')">All Levels</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Kindergarten')">Kindergarten</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Primary School')">Primary School</a></li> 
 
    <li><a href="#" onclick="selectedLvl('School Years 7,8,9')">School Years 7,8,9</a></li> 
 
    <li><a href="#" onclick="selectedLvl('GCSE – Middle School')">GCSE – Middle School</a></li> 
 
    <li><a href="#" onclick="selectedLvl('HKDSE')">HKDSE</a></li> 
 
    <li><a href="#" onclick="selectedLvl('IB, AS/A, Senior Secondary')">IB, AS/A, Senior Secondary</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Tertiary Level')">Tertiary Level 
 
    <li><a href="#" onclick="selectedLvl('Test Preparation')">Test Preparation</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Computer/IT')">Computer/IT</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Language Classes')">Language Classes</a></li> 
 
    <li><a href="#" onclick="selectedLvl('Business Training')">Business Training</a></li> 
 
</ul>

+0

感謝的人你真棒即時通訊新的JavaScript,但你打開我的更多地關注編程。 Goodvibes歡呼 – user1516642

+0

嗨男人最後一個問題,我已經把它放在一個價值很好,因爲一旦他們點擊它現在像一個數組添加,但有一個問題是有什麼辦法,我們只能添加一次,一旦他們點擊它,他們不能添加在第二次點擊我不知道這是可能的 – user1516642

+0

你想只隱藏用戶的李? – depperm