首页 > 解决方案 > 选择器 - 选择时暂时禁用选项

问题描述

是否可以在选择选项处于活动状态时禁用它?我有一个选择器,其中有各种选项,每个选项都会触发 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>

标签: javascriptselector

解决方案


如果我对您的理解正确,您想在option它被选中后禁用它?

function updateSource(e) {
  const target = e[e.value];
  target.disabled = true;
}
<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>

还是应该只禁用当前选择的那个?

function updateSource(e) {
  Object.keys(e).forEach(k => e[k].disabled = false);
  const target = e[e.value];
  target.disabled = true;
}
<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>


推荐阅读