2 回答
TA贡献1839条经验 获得超15个赞
在附加新内容之前使用它来清除选择中的选项
document.getElementById('namaServis').options.length = 0;
TA贡献1864条经验 获得超2个赞
您应该在第一个选择的每次更改时删除除第二个选择中的第一个之外的所有选项。另外,您不应该在if和else if#namaServis块内多次引用相同的元素 ( ) :
function selectorView() {
var namaEl = document.getElementById('namaServis');
while (namaEl.childNodes.length > 2) { // remove all option except the first
namaEl.removeChild(namaEl.lastChild);
}
var jenisServis = document.getElementById('jenisServis').value;
if (jenisServis == 'ringan') {
var teks = ["Pemeriksaan lampu", "Pemeriksaan kelayakan ban", "Pemeriksaan roda", "Pemeriksaan rem", "Pemeriksaan oli", "Pemeriksaan busi", "Penyetelan Gas", "Pemeriksaan Rantai",
"Pemeriksaan Oli Gear", "Pemeriksaan Kopling"];
teks.forEach(function (el) {
var option = document.createElement('option');
option.innerText = el;
namaEl.appendChild(option);
});
//console.log(namaServis);
}
else if (jenisServis == 'berat') {
var teks = ["Pemeriksaan kebocoran klep", "Membersihkan ruang bakar & piston", "Skur Klep"];
teks.forEach(function (el) {
var option = document.createElement('option');
option.innerText = el;
namaEl.appendChild(option);
});
//console.log(jenisServis.length);
}
}
<label for="jenisServis">Jenis Servis</label><br>
<select onchange="selectorView()" name="jenisServis" id="jenisServis">
<option value="" disabled selected>Choose one..</option>
<option value="berat">Berat</option>
<option value="ringan">Ringan</option>
</select><br>
<label for="namaServis">Nama Servis</label><br>
<select name="namaServis" id="namaServis">
<option value="" disabled selected>Choose one..</option>
</select>
- 2 回答
- 0 关注
- 75 浏览
添加回答
举报