如何比较字符串A1和数组值中的值A2(索引为0)?

时间:2019-02-09 21:35:20

标签: javascript arrays

我正在尝试比较以下内容: 选择时,字符串A1必须与选择时的字符串A2相匹配 如果选择了字符串A1且选择了字符串B1或C1或D1,则为false 如果选择了字符串B1,则它必须与字符串B2相匹配,其他A2,C2,D2为假

所以如果我有一个array1和array2 {“ A1”,“ B1”,“ C1”,“ D1”,“ E1”} {“ A2”,“ B2”,“ C2”,“ D2”,“ E2”}

如果我从下拉菜单中选择“ A1”和“ B2”,则arr [0]!= arr2 [0]然后为false

我不确定如何将索引与2个数组中的索引进行比较。

谢谢

1 个答案:

答案 0 :(得分:1)

var select1 = document.querySelector("#select1");
var select2 = document.querySelector("#select2");
var result = document.querySelector("#result");

select1.addEventListener("change", onChange);
select2.addEventListener("change", onChange);

var array1 = ["A1", "B1", "C1", "D1", "E1"];
var array2 = ["A2", "B2", "C2", "D2", "E2"];

for (var index in array1) {
  var option = document.createElement("option");
  option.textContent = array1[index];
  option.value = index;
  select1.appendChild(option);
}

for (index in array2) {
  option = document.createElement("option");
  option.textContent = array2[index];
  option.value = index;
  select2.appendChild(option);
}

function onChange() {
  result.value = select1.value === select2.value;
}

onChange();
<select id="select1"></select>
<select id="select2"></select>
<label for="result">Result:</label>
<input type="text" name="result" id="result" readonly />