gpt4 book ai didi

javascript - 选择器 - 选择时暂时禁用选项

转载 作者:行者123 更新时间:2023-12-01 01:57:28 28 4
gpt4 key购买 nike

是否可以在选择选项处于事件状态时禁用该选项?我有一个选择器,其中有各种选项,每个选项都会触发一个 SQL 查询。如果相同的选项被选择两次, map 就会中断。我认为最简单的解决方案是在选择选项后将其禁用。

<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>

最佳答案

如果我理解正确的话,您想在选择某个选项后将其禁用吗?

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>

关于javascript - 选择器 - 选择时暂时禁用选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50907059/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com