多个嵌套选择下拉列表依赖于JavaScript中的输入元素
我将输入元素作为范围多年。我可以通过按钮更改年度价值。我想在第二个选择中创建嵌套列表,这些列表取决于范围输入和第一次选择 - ID Countrysect。
<input type="range" id="yearRange" min="0" max="3" list="yearsDatalist" value="3"
onChange="changeYear(value)" onInput="changeYearLite(value)">
<datalist id="yearsDatalist">
<option value="0" label="1918"></option>
<option value="1" label="1936"></option>
<option value="2" label="1950"></option>
<option value="3" label="1981"></option>
</datalist>
然后我有两个选择的元素。第一个是静态的 - 不需要更改那里的值:
<select id="countrySelect">
<option value="0">Sweden</option>
<option value="1">Germany</option>
<option value="2">USA</option>
</select>
第二选择取决于年份和第一个选择中的值id = countryselect
<select id="itemSelection"></select>
我在某些不同的项目中使用了类似的对象:
const yearsFilterDataList = {
"1918": {
"0": "Choose model",
"volvo": "Volvo",
"mercedes": "Mercedes",
"saab": "Saab"
}, ...
I have input element as range for years. I can change value of year by buttons. I would like to create nested list in second select that depends on year from range input and on first select - id countrySelect.
<input type="range" id="yearRange" min="0" max="3" list="yearsDatalist" value="3"
onChange="changeYear(value)" onInput="changeYearLite(value)">
<datalist id="yearsDatalist">
<option value="0" label="1918"></option>
<option value="1" label="1936"></option>
<option value="2" label="1950"></option>
<option value="3" label="1981"></option>
</datalist>
Then I have two select elements. The first one is static - not necessary to change the values there:
<select id="countrySelect">
<option value="0">Sweden</option>
<option value="1">Germany</option>
<option value="2">USA</option>
</select>
Second select is dependent on year and on values in first select with id=countrySelect
<select id="itemSelection"></select>
I used object for values like this in some different project:
const yearsFilterDataList = {
"1918": {
"0": "Choose model",
"volvo": "Volvo",
"mercedes": "Mercedes",
"saab": "Saab"
}, ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用a 在
QuerySelector
中找到所选选项上的标签属性。下次尝试以更好的方式提供和组织您的代码(HTML,CSS,JS),以便人们不必猜测您的设置是什么。您还没有提供完整的
年filterdatalist
对象,因此滑块仅在第一个tick上填充选择表单。但这是一个假定的实现:
I would suggest using a template literal for string interpolation in a
querySelector
to find the label attribute on the selected option.Next time try to provide and organize your code (HTML, CSS, JS) in a better way so people don't have to guess what your setup exactly is. You also didn't provide the full
yearsFilterDataList
object so the slider only populates the select form on the first tick.But here is an assumed implementation:
我创建了一个小提琴,我为每个年度国家对创建
optgroup
,每个optgroup都有两个元素,具体取决于年度和国家/地区的ID(您当然可以更改每个选项optgroup
,这种选项
定义的唯一原因是简化我的测试)。更改yearge
或laydatalist
触发ChangeOptions
的的值,找到这些元素optgroup
要显示,然后循环optgroup,并隐藏所有与ID
匹配的内容。将显示匹配的optgroup
,并且,由于任何先前选择在逻辑上都是无效的,因此我更新itemselection.value
to所选第一个孩子的值optgroup < /代码>。
最后,我无论如何我都会调用
更改
,以便甚至在触发任何触发器之前初始化适当的状态。I have created a fiddle where I have created an
optgroup
for each year-country pair, each optgroup has two elements, depending on the ids of the year and country (you can of course change the options of eachoptgroup
, the lone reason for this kind ofoption
definition was to ease my testing). Changing the value ofyearRange
oryearsDatalist
triggers a call tochangeOptions
, which finds these elements, defines theid
of theoptgroup
to show, then loops the optgroups and hides everything except the one that matches theid
. The matchingoptgroup
will be shown and, since any prior selection is logically invalidated, I updateitemSelection.value
to the value of the first child of the chosenoptgroup
.Finally I call
changeOptions
anyway, in order to initialize a proper state even before any of the triggers are triggered.