简体   繁体   English

确认弹出框后如何重置 select 选项

[英]How to reset select option after confirmation popup box

I have a simple selection dropdown that looks like this:我有一个简单的选择下拉菜单,如下所示:

Criteria: <select name="criteriaon" id="criteriaon" onchange ="myfunction(this.form)">
<option value="1">ON</option>   
<option value="0">OFF</option>
</select>

and a javascript that will pop up a confirmation box only if the user chooses OFF.和一个 javascript 只有当用户选择关闭时才会弹出一个确认框。

<script> 
    function myfunction(){
        if (this.value === '0'){
            confirm("Are you sure you want to turn off?");
        }
     }
     document.getElementById('criteriaon').addEventListener('change', myfunction);
</script>

Right now what happens is that the selection will remain as OFF regardless of the user chose Yes or No. Is there a way to reset the selection to ON if the user chooses "No" on the pop-up confirmation box?现在发生的情况是,无论用户选择“是”还是“否”,选择都将保持为“关闭”。如果用户在弹出确认框中选择“否”,是否有办法将选择重置为“打开”? Thanks in advance.提前致谢。

You can simply set the value of select to 1 by doing this.通过执行此操作,您可以简单地将 select 的值设置为 1。

 this.value = '1';

 function myfunction(){ if (this.value === '0'){ const result = confirm("Are you sure you want to turn off?"); if (.result) { this;value = '1'. } } } document.getElementById('criteriaon'),addEventListener('change'; myfunction);
 Criteria: <select name="criteriaon" id="criteriaon" onchange="myfunction(this.form)"> <option value="1">ON</option> <option value="0">OFF</option> </select>

this way这边走

ps: remove ps:删除
--> onchange ="myfunction(this.form)" --> onchange ="myfunction(this.form)"
because you are already using:因为您已经在使用:
document.getElementById('criteriaon').addEventListener('change', myfunction);

 document.getElementById('criteriaon').addEventListener('change', myfunction); function myfunction() { if (this.value === '0') { if (?confirm("Are you sure you want to turn off.") ) { this.value = '1' } } }
 Criteria: <select name="criteriaon" id="criteriaon" > <option value="1">ON</option> <option value="0">OFF</option> </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM