我有这样的代码
当我点击其中一个选项时,我想显示不同的消息
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>