我如何使用 enable/disable 按钮的选项制作 <select>

How ca i make a <select> with options that enable/disable a button

i'm trying to do a multiple choice question that when selected the right answer enable a button

function enAble() {
  let possibleAns = document.getElementById('question').value;

  if (possibleAns != 1) {
    document.getElementById("btton").disabled = true;
  } else if (possibleAns = 1) {
    document.getElementById("btton").disabled = false;
  }
}
<select id="question" onchange="enAble()">
  <option>wrong answer</option>
  <option value="1">right answer</option>
  <option>wrong answer</option>
</select>

<button id="btton" type="button" disabled>button</button>

改变可能的答案 !=1 到可能的答案 !==1

在 else 语句中也使用 possibleAns ===1

else if (possibleAns = 1) {中,(possibleAns = 1)是赋值语句。使用 else if (possibleAns == 1) { 检查是否相等。

最简洁的编码方式(恕我直言)。

const 
  select_element = document.getElementById('question')
, button_element = document.getElementById("btton")
  ;
select_element.onchange = () =>
  {
  button_element.disabled = (select_element.value != '1')
  }
<select id="question">
  <option value="0">wrong answer</option>
  <option value="1">right answer</option>
  <option value="2">wrong answer</option>
</select>

<button id="btton" type="button" disabled>
  button
</button>

这可以通过将按钮禁用 属性 设置为值比较的结果来简化:

btton.disabled = this.value !== '1'

注意值是一个字符串,所以我们将它与另一个字符串而不是数字进行比较。

运行 查看其工作原理的代码段:

<select onchange="btton.disabled = this.value !== '1'">
  <option>wrong answer</option>
  <option value="1">right answer</option>
  <option>wrong answer</option>
</select>

<button id="btton" type="button" disabled>button</button>