gpt4 book ai didi

Javascript 隐藏/显示部分在重置时不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:46 25 4
gpt4 key购买 nike

我根据一些选项选择显示和隐藏一个 div。

有一个带有“启用和禁用”选项的下拉列表。一旦用户选择启用需要显示 div,否则隐藏 div。

基于下拉选项选择隐藏/显示 div 工作正常,但是如果 Disabled 是配置值。当我加载页面时,div 将被隐藏(工作正常),然后我将选择启用,然后 div 显示但是......当我点击重置选项时,下拉选项返回到已禁用,但应该隐藏的 Div 部分没有隐藏

这里我有这样的代码,保护开关是从 flask 接收到的 wtform 字段

{{form.protection_switch}}
<div align="center" >
<button class="btn btn-info btn-sm" type="submit"><i class="icon-ok bigger-110"></i>Submit</button>
&nbsp; &nbsp; &nbsp;
<button class="btn btn-sm" type="reset"><i class="icon-undo bigger-110"></i>Reset</button>
</div>

部分应该隐藏或显示

<div id="protection_data"></br> 
<table id="grid-table"></table>
<div id="grid-pager"></div>
</div>

J查询代码

function protection_selected() {
if ($('#protection_switch option:selected').val() == '0') {
$('#protection_data').hide();
} else if ($('#protection_switch option:selected').val() == '1') {
$('#protection_data').show();
}
}

$(document).ready(function() {
$('#protection_switch').change(function() {
protection_selected();
});
});
window.onload = protection_selected;

最佳答案

聆听重置时的点击。

function protection_selected() {
var isVisible = $('#protection_switch').val() == '1';
$('#protection_data').toggle(isVisible);
}

$(function(){ //document ready
$("#protection_switch") //get your select element
.on("change", protection_selected) //listen for change event
.change(); //trigger the change event so defaults can be set
$('input:reset').on("click", function(e){ //bind click event to reset button
this.form.reset(); //force reset so we guarantee it has finished running
protection_selected(); //run the update code
e.preventDefault(); //cancel the click since we already ran the reset code
});
});

reset 背后的痛苦是你可以检测到它何时被调用,但是在它成功运行之后没有任何事件。这就是我捕获点击、在表单上运行重置、调用您的函数并取消点击的原因。另一种方法是在内部使用延迟而不是调用函数,但这会导致竞争条件。

关于Javascript 隐藏/显示部分在重置时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23657795/

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