html
<form id="langList">
<select class="langOptions dropdown py-0" id="langSelect">
option class="lang" value="Language">Language</option>
option class="lang" value="es" selected>Spanish</option>
<option class="lang" value="fr">French</option>
<option class="lang" value="it">Italian</option>
</select>
</form>
JS-
cardsToTranslate.forEach((cardToTranslate) => {
const x = document.getElementById('langSelect');
x.addEventListener('change', event => {
langTo = event.target.value;
目前,上面的代码允许我从列表中选择我想要的语言。然而,我想将西班牙语作为默认语言,并且只能在意大利语和法语之间进行选择。将感谢您的帮助。
const changeLang = (e) => {
const value = e.target.value;
console.log(value)
}
<form id="langList">
<select class="langOptions dropdown py-0" id="langSelect" onchange="changeLang(event)">
<option class="lang" value="" disabled>Select language</option>
<option class="lang" value="es" selected="true" disabled>Spanish</option>
<option class="lang" value="fr">French</option>
<option class="lang" value="it">Italian</option>
</select>
</form>
您缺少禁用的属性,为了不允许用户选择西班牙语选项,我也为选择语言添加了相同的属性。