我在解决javascript的问题中堆叠,我需要询问有关我的项目的建议。我有一个名为主题和级别的输入,在级别我想要添加多个通过逗号分隔的值,
我创建了这个功能
function selectedLvl($levels){
lvl1.setAttribute("value", $levels);
}
在我的弹出窗口中,我有这个代码,用户点击它并更新输入。目前它的工作但只有一个值。如何添加以逗号分隔的多个密钥,例如,如果他们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>
答案 0 :(得分:1)
我相信你希望lvl1是一个数组,或者是一个值是数组的对象。
将lvl1作为数组:
var lvl1=[];
function selectedLvl($levels){
lvl1.push( $levels);
console.log(lvl1);
}
&#13;
<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>
&#13;
以lvl1作为对象:
var lvl1={};
lvl1.values=[];
function selectedLvl($levels){
var temp=lvl1.values;
temp.push( $levels);
lvl1.values=temp;
console.log(JSON.stringify(lvl1));
}
&#13;
<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>
&#13;