gpt4 book ai didi

javascript - Materialise - 如何在多重选择中动态选择选项?

转载 作者:行者123 更新时间:2023-12-02 21:10:58 28 4
gpt4 key购买 nike

我有一个多重选择元素,我想动态选择选项的混合,如下所示:

我能够动态选择一个选项,但仅此而已,

document.getElementById('update_days_of_the_week').selectedIndex = 2;
document.getElementById('update_days_of_the_week').selectedIndex = 3;
<div class="row">
<div class="input-field col s4">
<select id="update_days_of_the_week" multiple>
<option value="" disabled selected>Choose your days of the week</option>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
<label>Days of the week</label>
</div>
</div>

上面的代码,显然只选择了索引3,因为selectedIndex = 2被替换了。

最佳答案

使用jQuery你可以做这样的事情:

$('#update_days_of_the_week').val(['Monday', 'Friday'])
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<select id="update_days_of_the_week" multiple>
<option value="" disabled selected>Choose your days of the week</option>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>

关于javascript - Materialise - 如何在多重选择中动态选择选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61096057/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com