如何创建一组从属选择列表,其中连续列表依赖于前面列表选项的选择
How can I create a set of dependent select lists where successive lists depend on the choice of preceding list choices?
编辑底部的注释。
我要做的是创建一个脚本,该脚本允许我查看select
元素中的选定选项,并将该选择用作第二个select
元素中的选项可以使用的限制器。
虽然该示例显示了我所谈论内容的非常简单的说明,但我实际上将使用代码来限制较大选项列表的选项。在更大的代码中,我想让option_1_1
将选项从selectElement2
限制为通过options_2_9
options_2_1
,option_1_2
限制选择option_2_1
到option_2_11
,option_1_3
限制选择option_2_1
到option_2_5
。
举个例子;
<!DOCTYPE html>
<html>
<head>
<!-- I prefer not to use PHP, but if that's the ONLY way to do it, say so. -->
<script>
/* Need script to perform */
</script>
</head>
<body>
<!-- Select element 1 -->
<select id="selectElement1">
<!-- If option_1_0 is selected, no options are available in selectElement2 -->
<option id="option_1_0" value="" selected></option>
<!-- If option_1_1 is selected, only select options are available in selectElement2 -->
<option id="option_1_1" value="1" >1</option>
<!-- If option_1_2 is selected, only select options are available in selectElement2 -->
<option id="option_1_2" value="2" >2</option>
<!-- If option_1_3 is selected, only select options are available in selectElement2 -->
<option id="option_1_3" value="3" >3</option>
</select>
<!-- Select element 2 -->
<select id="selectElement2">
<!-- Selected by default and cannot change unless a choice is selected from selectElement1 -->
<option id="option_2_0" value="" selected></option>
<!-- If option_1_1 is selected, only option_2_1 is available -->
<option id="option_2_1" value="1">1</option>
<!-- If option_1_2 is selected, only option_2_1 and option_2_2 are available -->
<option id="option_2_2" value="2" >2</option>
<!-- If option_1_3 is selected, only option_2_3 is available -->
<option id="option_2_3" value="3" >3</option>
<!-- If option_1_3 is selected, only option_2_3 and option_2_4 are available -->
<option id="option_2_4" value="4" >4</option>
</select>
</body>
</html>
编辑谢谢大家。你们都帮了我很多。这是最终的工作代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
<script type="text/javascript">
var gradeValue = {};
gradeValue['A'] = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
gradeValue['B'] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'];
gradeValue['C'] = ['1', '2', '3', '4', '5'];
function changeList() {
var gradeList = document.getElementById("grade");
var rankingList = document.getElementById("ranking");
var selGrade = gradeList.options[gradeList.selectedIndex].value;
while (rankingList.options.length) {
rankingList.remove(0);
}
var rank = gradeValue[selGrade];
if (rank) {
for (var i = 0; i < rank.length; i++) {
var ranks = new Option(rank[i], i);
rankingList.options.add(ranks);
}
}
}
</script>
</head>
<body>
<select id="grade" onchange="changeList()">
<option value="">-- Select --</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<select id="ranking"></select>
</body>
</html>
你可以尝试这样的事情。
var selectOne = document.getElementById('selectElement1'),
selectTwo = document.getElementById('selectElement2'),
//set up options (key is equal to the value of the option in selectElement1)
optsList = {
1: ['option_2_1', 'option_2_2', 'option_2_3', 'option_2_4'],
2: ['option_2_5', 'option_2_6', 'option_2_7', 'option_2_8'],
3: ['option_2_9', 'option_2_10', 'option_2_11', 'option_2_12']
};
//add an event listener for the onchange event
selectOne.onchange = function (e) {
var selectedOpt = this[e.target.selectedIndex],
optListSet = optsList[selectedOpt.value] || 0;
//disable all options.
for(var i=0; i < selectTwo.length; i++){
selectTwo[i].disabled = "disabled";
}
//reset selected option
selectTwo.children[0].selected = "selected";
//loop through the options set corresponding to the value of the selected option
if(optListSet){
for (var i = 0; i < optListSet.length; i++) {
opt = document.getElementById(optListSet[i]);
opt.disabled = false;
}
}
//enable options list 2
selectTwo.disabled = false;
};
这是jsfiddle
在菜单上添加更改侦听器。然后使用 disabled
属性来防止选择不需要的选项:
var suffix = jQuery(selectedOption).attr("id").split("_")[2]; // for option_2_3, this hsould be "3"
// loop through all option elements, add a css class to differentiate them from other options.
for(...) {
// test if the id matches
var currentIdSuffix = jQuery( currentOption ).attr("id").split("_")[2];
if( currentIdSuffix != suffix ) {
// add disabled attribute = prevents selection
jQuery( currentOption ).attr("disabled", "disabled");
}
}
相关文章:
- Angular:使用选择列表选择过滤代码中的对象
- JavaScript类列表选择器错误
- 将下拉列表“选择值”传递到 URL
- 禁用基于下拉列表选择 asp.net JavaScript 启用文本框
- 下拉列表<选择>为列表.js
- 经典 ASP - 下拉列表:选择与第一个下拉列表具有相同值的所有下拉列表
- 映射下拉列表选择到URL
- 删除基于先前下拉列表选择的“选择下拉列表”选项
- 在下拉列表选择中显示模态
- 下拉列表选择的值在UI淘汰js中没有更新
- 如何将 ID 分配给动态下拉列表选择
- 使用 dojo 将鼠标悬停在动态下拉列表选择上时添加工具提示
- 链接下拉列表选择列表代码 -- 连接数据
- Javascript 不遵循下拉列表选择
- 检索引导下拉列表选择文本
- 表单事件更改不适用于下拉列表选择
- 无法检测加载时的选择/下拉列表选择事件
- 将 CakePHP 命名参数添加到列表选择的 URL 中
- 为什么聚合物核心列表选择中的切换操作不正确
- 为列表选择类并仅设置其中一个类