选择器 - 在选中时暂时禁用选项

时间:2018-06-18 10:07:44

标签: javascript selector

是否可以在激活时禁用选择选项?我有一个选择器,它有各种选项,每个选项触发一个SQL查询。如果两次选择相同的选项,则地图会中断。我认为最简单的解决方案是在选择后禁用选项。

<select id="sql" onclick="updateSource()" class="selector"> 
    <option value="SELECT * FROM indicators WHERE 1=0" disabled selected>Select an Indicator</option>     
    <option value="SELECT * FROM indicators WHERE grain ILIKE 'Yes'">Change in grain (maize, wheat & barley) production</option>
    <option value="SELECT * FROM indicators WHERE sorghum ILIKE 'Yes'">Change in Sorghum production</option>
</select>

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您希望在选择后禁用option吗?

&#13;
&#13;
function updateSource(e) {
  const target = e[e.value];
  target.disabled = true;
}
&#13;
<select onchange="updateSource(this)">
  <option value="0" disabled selected>Select an Indicator</option>
  <option value="1">Change in grain (maize, wheat & barley) production</option>
  <option value="2">Change in Sorghum production</option>
</select>
&#13;
&#13;
&#13;

或者它应该只禁用当前选择的那个吗?

&#13;
&#13;
function updateSource(e) {
  Object.keys(e).forEach(k => e[k].disabled = false);
  const target = e[e.value];
  target.disabled = true;
}
&#13;
<select onchange="updateSource(this)">
  <option value="0" disabled selected>Select an Indicator</option>
  <option value="1">Change in grain (maize, wheat & barley) production</option>
  <option value="2">Change in Sorghum production</option>
</select>
&#13;
&#13;
&#13;