Tengo dos listas desplegables y quiero, por ejemplo, si value=fetch seleccionado, mostrar el segundo menú desplegable pero no funciona.
Lo intenté con llamar a un archivo PHP externo pero me quedé atascado nuevamente. Creo que el problema es cuando llamo al método de cambio pero no sé qué escribir.
function action(dropdown) { var value = dropdown.options[dropdown.selectedIndex].value; if (value == 'Fetch'){ document.getElementById("student_list").style.display = "block"; document.getElementById("subject_list").style.display = "none"; } if(value == 'Count'){ document.getElementById("student_list").style.display = "block"; } if(value == 'Percentage'){ document.getElementById("subject_list").style.display = "block"; } } <body> <div class="main_container"> <form method="post"> <select name="action" id ="fetch" onchange="action(this.value);"> <option value="" id="action">--Select Action--</option> <option value="Fetch">Fetch</option> <option value="Count">Count</option> <option value="Percentage">Percentage</option> </select> <select name="student_name" id ="student_list" onChange="" style="display:none;"> <!--onchange="getNext(this.value);" --> <option value="">--Select Action --</option> <option value="All">All Students</option> <option value="NameS">Student Name</option> </select> </form> </div>No puede usar una función llamada " acción ", consulte https://stackoverflow.com/a/37590405/5391965
Debe recuperar sus valores dentro de la función en lugar de pasarlos en parámetros como este:
function displayStudentList(dropdown) { let subject = document.getElementById("subject_list").value; if (subject === 'Fetch') { document.getElementById("student_list").style.display = "block"; document.getElementById("subject_list").style.display = "none"; } if (subject === 'Count') { document.getElementById("student_list").style.display = "block"; } if (subject === 'Percentage') { document.getElementById("subject_list").style.display = "block"; } } <div class="main_container"> <form method="post"> <select name="action" id="subject_list" onchange="displayStudentList()"> <option value="" id="action">--Select Action--</option> <option value="Fetch">Fetch</option> <option value="Count">Count</option> <option value="Percentage">Percentage</option> </select> <select name="student_name" id ="student_list" onChange="" style="display:none;"> <!--onchange="getNext(this.value);" --> <option value="">--Select Action --</option> <option value="All">All Students</option> <option value="NameS">Student Name</option> </select> </form> </div>