如何显示不同的窗口取决于我从选择组合中选择的选项?



我有这样的代码

当我点击其中一个选项时,我想显示不同的消息

document.getElementById("specialityAppointmentSelection").addEventListener('onchange', function(){
let specialityAppointmentSelection = document.getElementById("specialityAppointmentSelection");
let specialityOption = specialityAppointmentSelection.value;
console.log(specialityOption);
if(specialityOption === "1"){
console.log("medicina");
} else {
console.log("enfermería");
}
});
<p>Selecciona el tipo de cita que desee: </p>
<select id = "specialityAppointmentSelection">
<option value = "1">Medicina</option>
<option value = "2">Enfermería</option>
</select>

但是我的console.log没有显示任何东西,是什么问题?

您应该使用change而不是像.addEventListener('change', function(){那样使用onchange

document.getElementById("specialityAppointmentSelection").addEventListener('change', function(){
let specialityAppointmentSelection = document.getElementById("specialityAppointmentSelection");
let specialityOption = specialityAppointmentSelection.value;
console.log(specialityOption);
if(specialityOption === "1"){
console.log("medicina");
} else {
console.log("enfermería");
}
});
<p>Selecciona el tipo de cita que desee: </p>
<select id = "specialityAppointmentSelection">
<option value = "1">Medicina</option>
<option value = "2">Enfermería</option>
</select>

相关内容

  • 没有找到相关文章