我有两个下拉列表,第二个依赖于第一个.我不知道的是如何为 node 添加不同的值.值(选项元素的值).

编辑:现在我在textContent和html选项元素中有相同的值,但我需要添加不同于textContent的值.

var moduly = [];
moduly['1'] = ["Inhabitants", "Married"];
moduly['2'] = ["German", "French", "English"];
moduly['3'] = ["Houses", "Estates"];
moduly['4'] = ["Surface area", "Meadow area"];

document.querySelector("select[name='modulSelect']").addEventListener("change", function() {
  var element = moduly[this.value.toString().toLowerCase()];
  var elementvalue = mvalue[this.value.toString().toLowerCase()];
  if (element) {
    var select = document.querySelector("select[name='itemSelect']").cloneNode();
    var node = document.createElement("option");
    node.value = 0;
    node.setAttribute("disabled", true);
    node.setAttribute("selected", true);
    node.textContent = "Choose module";
    select.appendChild(node);
    moduly[this.value.toString().toLowerCase()].forEach(function(element) {
      var node = document.createElement("option");
      node.value = element;
      node.textContent = element;
      select.appendChild(node);
    });
    document.querySelector("select[name='itemSelect']").parentElement.replaceChild(select, document.querySelector("select[name='itemSelect']"));
  }
}, false);
<form>
  <select id="modulSelect" name="modulSelect">
    <option value="1">Demographics</option>
    <option value="2">Nation</option>
    <option value="3">Property</option>
    <option value="4">Area</option>
  </select>
</form>

推荐答案

您使用的变量未定义,这会引发错误:

var elementvalue = mvalue[this.value.到String().到LowerCase()];

由于var elementvalue也未使用,因此您可以注释该行或将其删除.此外,您try 克隆一个名为itemSelectselect元素,该元素在本示例代码中不存在.缺少以下行:

<select id="itemSelect" name="itemSelect"></select>

修复这两个错误,您的代码应按预期工作.

Working example:(我在第二个select中添加了一个禁用的option,以便您可以 Select 第一个option"人口统计",并将第二个禁用的option从" Select 模块"重命名为" Select 项目",以便您可以区分两者)

var moduly = [];
moduly['1'] = ["Inhabitants", "Married"];
moduly['2'] = ["German", "French", "English"];
moduly['3'] = ["Houses", "Estates"];
moduly['4'] = ["Surface area", "Meadow area"];

document.querySelec到r("select[name='modulSelect']").addEventListener("change", function() {
  var element = moduly[this.value.到String().到LowerCase()];
  //var elementvalue = mvalue[this.value.到String().到LowerCase()];
  if (element) {
    var select = document.querySelec到r("select[name='itemSelect']").cloneNode();
    var node = document.createElement("option");
    node.value = 0;
    node.setAttribute("disabled", true);
    node.setAttribute("selected", true);
    node.textContent = "Choose item";
    select.appendChild(node);
    moduly[this.value.到String().到LowerCase()].forEach(function(element) {
      var node = document.createElement("option");
      node.value = element;
      node.textContent = element;
      select.appendChild(node);
    });
    document.querySelec到r("select[name='itemSelect']").parentElement.replaceChild(select, document.querySelec到r("select[name='itemSelect']"));
  }
}, false);
<form>
  <select id="modulSelect" name="modulSelect">
    <option value="0" disabled selected>Choose module</option>
    <option value="1">Demographics</option>
    <option value="2">Nation</option>
    <option value="3">Property</option>
    <option value="4">Area</option>
  </select>
  
  <select id="itemSelect" name="itemSelect"></select>
</form>


如果需要不同的值/内容对,可以将moduly数组嵌套得更深一步.更改数组,例如从:

moduly['3'] = ["Houses", "Estates"];

moduly['3'] = [["10", "Houses"], ["11", "Estates"]];

Then you just need 到 output the data with array notation:

node.value = element[0];
node.textContent = element[1];

var moduly = [];
moduly['1'] = [["5", "Inhabitants"], ["6", "Married"]];
moduly['2'] = [["7", "German"], ["8", "French"], ["9", "English"]];
moduly['3'] = [["10", "Houses"], ["11", "Estates"]];
moduly['4'] = [["12", "Surface area"], ["13", "Meadow area"]];

document.querySelec到r("select[name='modulSelect']").addEventListener("change", function() {
  var element = moduly[this.value.到String().到LowerCase()];
  //var elementvalue = mvalue[this.value.到String().到LowerCase()];
  if (element) {
    var select = document.querySelec到r("select[name='itemSelect']").cloneNode();
    var node = document.createElement("option");
    node.value = 0;
    node.setAttribute("disabled", true);
    node.setAttribute("selected", true);
    node.textContent = "Choose item";
    select.appendChild(node);
    moduly[this.value.到String().到LowerCase()].forEach(function(element) {
      var node = document.createElement("option");
      node.value = element[0];
      node.textContent = element[1];
      select.appendChild(node);
    });
    document.querySelec到r("select[name='itemSelect']").parentElement.replaceChild(select, document.querySelec到r("select[name='itemSelect']"));
  }
}, false);
<form>
  <select id="modulSelect" name="modulSelect">
    <option value="0" disabled selected>Choose module</option>
    <option value="1">Demographics</option>
    <option value="2">Nation</option>
    <option value="3">Property</option>
    <option value="4">Area</option>
  </select>
  
  <select id="itemSelect" name="itemSelect"></select>
</form>

Javascript相关问答推荐

更新Reduxstore 中的状态变量,导致整个应用程序出现不必要的重新渲染

如何使用JavaScript动态地将CSS应用于ToDo列表?

为什么我达到了时间限制!?LeetCode链接列表循环(已解决,但需要解释!)

禁用从vue.js 2中的循环创建的表的最后td的按钮

如何判断属于多个元素的属性是否具有多个值之一

从实时数据库(Firebase)上的子类别读取数据

当运行d3示例代码时,没有显示任何内容

在观察框架中搜索CSV数据

数字时钟在JavaScript中不动态更新

为什么Mutations 观察器用微任务队列而不是macrotask队列处理?

实现JS代码更改CSS元素

在使用HighChats时如何避免Datatables重新初始化错误?

使用Java脚本导入gltf场景并创建边界框

映射类型定义,其中值对应于键

try 使用PM2在AWS ubuntu服务器上运行 node 进程时出错

将对象推送到数组会导致复制

警告框不显示包含HTML输入字段的总和

为列表中的项目设置动画

Angel Auth Guard-用户只有在未登录时才能访问登录页面,只有在登录时才能访问其他页面

无法向甜甜圈图表上的ChartJSImage添加可见标签