如何使切换开关触发选择下拉列表以随机化其选项值?
我正在为角色生成器设置页面,并且我为用户提供了随机化性状或输入自己的特征的选项。我有“选择VS随机”选项设置为每个特征类别的切换开关,然后如果他们想选择自己的特征,我将为每个类别的下拉选择菜单,并列出了所有特征。
这是我的开关的HTML:
<div class="switch">
<p>Class</p>
<label>
My Choice
<input type="checkbox" id="class-choice">
<span class="lever red darken-4"></span>
Random
</label>
</div>
这是我的html相应的选择菜单:
<div class="input-field col s6">
<select id="classMenu" name="class">
<option value="barbarian">Barbarian</option>
<option value="bard">Bard</option>
<option value="cleric">Cleric</option>
<option value="druid">Druid</option>
<option value="fighter">Fighter</option>
<option value="monk">Monk</option>
<option value="paladin">Paladin</option>
<option value="ranger">Ranger</option>
<option value="rogue">Rogue</option>
<option value="sorcerer">Sorcerer</option>
<option value="warlock">Warlock</option>
<option value="wizard">Wizard</option>
</select>
<label for="class">Class</label>
</div>
我想发生的事情是,每次他们单击“ toggle switch to to tocal”“随机”时,相应的下拉菜单都会从列表中显示一个随机选项。我将所有选项名称保存为我的JavaScript中的数组:
var charClasses = ["barbarian", "bard", "cleric", "druid", "fighter", "monk", "paladin", "ranger", "rogue", "sorcerer", "warlock", "wizard"];
我知道我需要使用某种事件侦听器和数学随机函数,然后使用一些内容来显示该选项值,但是我想我对如何实际如何实际上感到困惑这样做。然后我想我必须使用$('#save-btn')。on(“ click”,...也将其保存到localstorage。我才开始学习JS,这对我来说并不是真正的直觉但是,任何指导都将不胜感激。
I'm setting up a page for a character generator, and I'm giving users the option to either randomize traits or enter their own. I have the "choice vs random" options set up as toggle switches for each trait category, and then if they want to choose their own, I have drop-down select menus for each category with all the traits listed.
Here's my HTML for the switches:
<div class="switch">
<p>Class</p>
<label>
My Choice
<input type="checkbox" id="class-choice">
<span class="lever red darken-4"></span>
Random
</label>
</div>
Here's my HTML for the corresponding select menu:
<div class="input-field col s6">
<select id="classMenu" name="class">
<option value="barbarian">Barbarian</option>
<option value="bard">Bard</option>
<option value="cleric">Cleric</option>
<option value="druid">Druid</option>
<option value="fighter">Fighter</option>
<option value="monk">Monk</option>
<option value="paladin">Paladin</option>
<option value="ranger">Ranger</option>
<option value="rogue">Rogue</option>
<option value="sorcerer">Sorcerer</option>
<option value="warlock">Warlock</option>
<option value="wizard">Wizard</option>
</select>
<label for="class">Class</label>
</div>
What I want to have happen is that every time they click the toggle switch to "random," the corresponding drop-down menu will display a randomized option from the list. I have all the option names saved as an array in my JavaScript:
var charClasses = ["barbarian", "bard", "cleric", "druid", "fighter", "monk", "paladin", "ranger", "rogue", "sorcerer", "warlock", "wizard"];
I know I need to use some sort of event listener and a math randomizing function and then something to display that option value, but I guess I'm confused about how to actually do that. And then I think I'll have to use a $('#save-btn').on("click", ... to save it to localStorage as well. I just started learning JS and it's not really intuitive for me yet, so any guidance would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获取随机数组项使用:
选择项目使用:
您可以使用这样的按钮事件:
代码从数组中选择一个随机值,然后放入变量项目中。接下来,在选择字段中选择项目。
To get a random item of array use:
To select the item use:
You can use a button event like this:
The code select a random value from array and put in variable item. Next select the item in select field.