如何更改选择列表js中的选项

我有一个选择列表的更改事件,我必须在用户选择一个选项后通过提示窗口询问用户,他要选择的另一个选项是什么,然后在提示窗口中介绍该值,选择的选项随新的而改变。


<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Examen modulo1</title>

    <script>


        function cambiaSelect() {

            var provincias = document.getElementById("provincias");

            var posicion = provincias.selectedIndex;

            var select = provincias.options[posicion].value;

            var pantalla;


            // when prompt value is 0

            if (provincias.options[posicion].value == 0) {

                alert("No has seleccionado ninguna provincia");

            } else {

                // when index is selected

                if (provincias.options[posicion].selected == true) {

                    pantalla = prompt("Has seleccionado la provincia: " + provincias.options[posicion].text + "\n Introduce otra provincia aqui:");

                }

                // when prompt do not receive a value

                if (pantalla == "") {

                    alert("El valor que has introducido no existe!\Intentalo de nuevo!");

                }

                //prompt equals the selected value

                if (select == pantalla && provincias.options[posicion].selected == true) {

                    alert("Esa provincia ya esta seleccionada");

                }

            }

        }//cambia

    </script>

</head>


<body>

4.Formulario de tipo select. Selecciona provincias:<br>


<form method="GET">


    <br>

    <select id="provincias">

        <option value="0">0.Selecciona...</option>

        <option value="1">1.Albacete</option>

        <option value="2">2.Murcia</option>

        <option value="3">3.Madrid</option>

    </select><br>

    <br>

    <input type="button" value="Cambiar" onclick="cambiaSelect()"><br>

    <br>

    <br>


</form>

</body>

</html>


Smart猫小萌
浏览 219回答 1
1回答

达令说

您可以通过单击“运行代码片段”来运行下面的代码。如果用户输入 3,选择的选项将变为“3.Madrid”。西班牙语让我很困惑,所以我把它换成了英语,但编程逻辑是一样的。<form method="GET">&nbsp; <br>&nbsp; <select id="provincias">&nbsp; &nbsp; <option value="0">0.Selecciona...</option>&nbsp; &nbsp; <option value="1">1.Albacete</option>&nbsp; &nbsp; <option value="2">2.Murcia</option>&nbsp; &nbsp; <option value="3">3.Madrid</option>&nbsp; </select>&nbsp; <br>&nbsp; <input type="button" value="Cambiar" onclick="cambiaSelect()"></form><script>&nbsp; function cambiaSelect() {&nbsp; &nbsp; var provinces = document.getElementById("provincias");&nbsp; &nbsp; var position = provinces.selectedIndex;&nbsp; &nbsp; var select = provinces.options[position].value;&nbsp; &nbsp; var pantalla;&nbsp; &nbsp; if (provinces.options[position].value == 0) {&nbsp; &nbsp; &nbsp; alert("You have not selected any province");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; if (provinces.options[position].selected == true) {&nbsp; &nbsp; &nbsp; &nbsp; pantalla = prompt("You have selected the province: " + provinces.options[position].text + "\n Choose another province here");&nbsp; &nbsp; &nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pantalla) { // this ensures that if the user clicks cancel on the prompt, we don't do any more logic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pantalla === "") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("The value you entered does not exist. Try again!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (select === pantalla) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("That province is already selected");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (pantalla >= 1 && pantalla <= 3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('provincias').selectedIndex = pantalla;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pantalla = prompt("Choose another province here");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { // if the cancel button on prompt was pressed, get out of loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }</script>唯一的问题是如果用户输入了一个介于 1 和 3 之间的不是整数的数字,比如 2.5。2.5 不是一个合适的索引,对吧?因此,如果您想处理这种情况,可以添加以下修改:else if (Number.isInteger(Number(pantalla)) && pantalla >= 1 && pantalla <= 3). 如果您有更多问题,请告诉我。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript