简体   繁体   English

实现如何更改第二个 select 选项取决于第一个?

[英]Materialize how to change second select option depended from the first?

I use materialize, and the problem is: I have 2 select-div with options, and when I choose Ukraine in the first select?我使用materialize,问题是:我有2个带选项的select-div,当我在第一个select中选择乌克兰时? The second select would show me only Lviv, Odessa, Kiev, kharkiv.第二个 select 将只显示利沃夫、敖德萨、基辅、哈尔科夫。 If I choose Poland in the first select, the second select would show me only Warsaw and Krakow.如果我在第一个 select 中选择波兰,第二个 select 将只显示华沙和克拉科夫。

I've already asked the same question, and received few answers which was useful, but when I linked materialize CSS, code doesn't work.我已经问过同样的问题,并且收到了一些有用的答案,但是当我链接具体化 CSS 时,代码不起作用。

<div class="input-field col s12">
    <select id="country-list" onchange="changeCountry(this)">
        <option value="none" disabled selected>Choose your option</option>
        <option value="1">Ukraine </option>
        <option value="2">Poland</option>
    </select>
</div>

<div class="input-field col s12">
    <select id="city-list">
        <option value="none" disabled selected>Choose your option</option>
        <option value="1">Lviv</option>
        <option value="1">Kiev</option>
        <option value="1">Kharkiv</option>
        <option value="1">Odessa</option>
        <option value="2">Krakow</option>
        <option value="2">Warsaw</option>
    </select>
</div>

And here JavaScript在这里 JavaScript

var countryObject = {
    Ukraine: ["Lviv", "Kiev", "Kharkiv", "Odessa"],
    Poland: ["Krakow", "Warsaw"],
};

function changeCountry() {
    document.getElementById("city-list").options.length = 0;

    var cityListArray =
        countryObject[document.getElementById("country-list").value];
    console.log(document.getElementById("city-list").options);

    for (var item = 0; item < cityListArray.length; item++) {
        document.getElementById("city-list").options[
            document.getElementById("city-list").options.length
        ] = new Option(cityListArray[item], cityListArray[item]);
    }
}

There is no need to continuously find the elements on the page.无需在页面上不断查找元素。 Find them once, and use the resulting element later when needed.找到它们一次,然后在需要时使用结果元素。

 const countryObject = { Ukraine: ["Lviv", "Kiev", "Kharkiv", "Odessa"], Poland: ["Krakow", "Warsaw"], }; const countryListEl = document.getElementById("country-list"); const cityListEl = document.getElementById("city-list"); const defaultOption = new Option("Choose your option"); defaultOption.disabled = true; defaultOption.selected = true; function changeCountry() { cityListEl.options.length = 0; cityListEl.options[0] = defaultOption; const cityListArray = countryObject[countryListEl.value]; for (let i = 0; i < cityListArray.length; i++) { cityListEl.options[i + 1] = new Option( cityListArray[i], cityListArray[i] ); } }
 <div class="input-field col s12"> <select id="country-list" onchange="changeCountry(this)"> <option value="none" disabled selected>Choose your option</option> <option value="Ukraine">Ukraine </option> <option value="Poland">Poland</option> </select> </div> <div class="input-field col s12"> <select id="city-list"> <option value="none" disabled selected>Choose your option</option> </select> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery如何根据“第三个选择列表”选项更改第二个选择列表,并根据第一个选择更改第二个选择列表 - How jQuery to change a second select list based on the Third select list option and second select based upon first 如何根据第一个选项选择第二个选项(两个选项都来自数据库) - How to select the second option based on the first option (both select option are from the database) 3个选择元素,它们根据第一选择和第二选择中的所选选项更改选项 - 3 select elements that change options based on the selected option in the first and second select 当值是动态的时,如何基于第一个选择列表选项更改第二个选择列表? - How to change a second select list based on the first select list option when values are dynamic? 如何使用 Materialise 触发选择更改 - How to trigger select change with Materialize AngularJS-如何在从第一选择中选择选项时填充第二选择? - AngularJS- How to populate second select on selection of option from first select? 如何填充第二 <select>根据第一<select>选项 - How to populate second <select> according to first <select> option 如何使用 Materialise 加载选择选项 - How to load a select option using Materialize 更改选择的第一个选项 - Change first option of select 动态下拉选择选项服务中的第一个选项,第二个选项将选择所选服务的医生。 - Dynamic dropdown select option first option from the service and the second option will select the doctor of the services selected.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM