在输入中弹出 onclick 时添加多个值 setAttribute
Adding Multiple value setAttribute on pop up onclick in an input
我正在解决问题 javascript,我需要就我的项目寻求建议。我有一个名为 Subject 和 Level 的输入,在级别中我想添加多个用逗号分隔的值,
我创建了这个函数
function selectedLvl($levels){
lvl1.setAttribute("value", $levels);
}
在我的弹出窗口中,我有这段代码,用户可以在其中单击它并更新输入。目前它的工作但只有一个值。我如何添加多个以逗号分隔的键,例如,如果他们点击每个值,它将附加到所有级别、幼儿园、小学等..
<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 是一个数组,或者一个值为数组的对象。
以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>
我正在解决问题 javascript,我需要就我的项目寻求建议。我有一个名为 Subject 和 Level 的输入,在级别中我想添加多个用逗号分隔的值,
我创建了这个函数
function selectedLvl($levels){
lvl1.setAttribute("value", $levels);
}
在我的弹出窗口中,我有这段代码,用户可以在其中单击它并更新输入。目前它的工作但只有一个值。我如何添加多个以逗号分隔的键,例如,如果他们点击每个值,它将附加到所有级别、幼儿园、小学等..
<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 是一个数组,或者一个值为数组的对象。
以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>