简体   繁体   English

为什么我的函数在按钮中的onClick事件上只能运行一次?

[英]Why does my function runs only once on onClick event in button?

I've searched, but could't find a solution to my problem. 我已经搜索过,但是找不到解决我问题的方法。 I'm not an JS expert, just a novice, but I can't figure out why this works only once. 我不是JS专家,只是新手,但我不知道为什么它只能工作一次。 I have a SELECT droplist and I wanted to execute different instructions on different option selected. 我有一个SELECT下拉列表,我想对所选的不同选项执行不同的指令。

I have 4 html blocks of text, only 1 is visible (display:block), the others are hidden (display:none). 我有4个html文本块,只有1个可见(display:block),其他隐藏了(display:none)。 What I want to achieve here is to show only the selected block and hide the others, when I click the button. 我想在这里实现的是,当我单击按钮时,仅显示选定的块,而隐藏其他块。

Here's the code I've got: 这是我得到的代码:

 function filter() { if (document.getElementById('dynamic_select').value == 'opt1') { document.getElementById('html_block_1').style.display = 'block'; document.getElementById('html_block_2').style.display = 'none'; document.getElementById('html_block_3').style.display = 'none'; document.getElementById('html_block_4').style.display = 'none'; } if (document.getElementById('dynamic_select').value == 'opt2') { document.getElementById('html_block_2').style.display = 'block'; document.getElementById('html_block_1').style.display = 'none'; document.getElementById('html_block_3').style.display = 'none'; document.getElementById('html_block_4').style.display = 'none'; } if (document.getElementById('dynamic_select').value == 'opt3') { document.getElementById('html_block_3').style.display = 'block'; document.getElementById('html_block_1').style.display = 'none'; document.getElementById('html_block_2').style.display = 'none'; document.getElementById('html_block_4').style.display = 'none'; } if (document.getElementById('dynamic_select').value == 'opt4') { document.getElementById('html_block_4').style.display = 'block'; document.getElementById('html_block_1').style.display = 'none'; document.getElementById('html_block_2').style.display = 'none'; document.getElementById('html_block_3').style.display = 'none'; } } 
 <select id="dynamic_select"> <option value="opt1">Display html block 1</option> <option value="opt2">Display html block 2</option> <option value="opt3">Display html block 3</option> <option value="opt4">Display html block 4</option> </select> <button onClick="filter()">Filter</button> 

When I select one of options and click the button, it works as it supposed to. 当我选择一个选项并单击按钮时,它会按预期工作。 But when I select other option and click the button for the second time - it does nothing. 但是,当我选择其他选项并第二次单击按钮时,它什么也没做。 The function executes only once. 该功能仅执行一次。

Is there a simple way to fix it? 有简单的解决方法吗?

Not quite related to the problem at hand, but if you change the values in the listbox to 1 to 4 you could simplify your filter function like 与当前问题不太相关,但是如果将列表框中的值更改为1到4,则可以简化过滤器功能,例如

function filter()
{
    let value = document.getElementById('dynamic_select').value;

    for (i = 1; i < 5; i++)
    {
        let id      = 'html_block_' + i;
        let display = i == value ? 'block' : 'none';
        document.getElementById( id ).style.display = display;
    }
}

The code is working as expected. 该代码按预期工作。 Try this. 尝试这个。 I created blocks with the given Ids. 我使用给定的ID创建了块。 We can always optimize this though. 但是我们总是可以优化它。

function filter() {
if (document.getElementById('dynamic_select').value == 'opt1'){
   document.getElementById('html_block_1').style.display = 'block';
   document.getElementById('html_block_2').style.display = 'none';
   document.getElementById('html_block_3').style.display = 'none';           
   document.getElementById('html_block_4').style.display = 'none';
   }
else if (document.getElementById('dynamic_select').value == 'opt2'){
   document.getElementById('html_block_2').style.display = 'block';
   document.getElementById('html_block_1').style.display = 'none';
   document.getElementById('html_block_3').style.display = 'none';           
   document.getElementById('html_block_4').style.display = 'none';
   }
else if (document.getElementById('dynamic_select').value == 'opt3'){
   document.getElementById('html_block_3').style.display = 'block';
   document.getElementById('html_block_1').style.display = 'none';
   document.getElementById('html_block_2').style.display = 'none';           
   document.getElementById('html_block_4').style.display = 'none';
   }
 else {
   document.getElementById('html_block_4').style.display = 'block';
   document.getElementById('html_block_1').style.display = 'none';
   document.getElementById('html_block_2').style.display = 'none';           
   document.getElementById('html_block_3').style.display = 'none';
   }
}
</script>

This should work. 这应该工作。 There's something else that's preventing your event to happen. 还有其他一些因素阻止您的活动发生。 If your filter function is in some custom js file, shift the function in your HTML file itself where you have your dropdown HTML code. 如果您的过滤器功能位于某些自定义js文件中,请在具有下拉HTML代码的HTML文件本身中移动该功能。 Then try running it. 然后尝试运行它。 Also put this console just after your filter function opening 还要在打开过滤器功能之后放置此控制台

console.log(document.getElementById('dynamic_select').value)

Also check this error in the console --> Cannot read property 'style' of null. 还要在控制台中检查此错误->无法读取null的属性“样式”。 Seems like your "html_block_index" element is not present in the DOM. 似乎您的“ html_block_index”元素不在DOM中。

Here is the updated snippet: Please check the below, It works as expected 这是更新的代码段:请检查以下内容,它按预期工作

 function filter() { if (document.getElementById('dynamic_select').value == 'opt1') { document.getElementById('html_block_1').style.display = 'block'; document.getElementById('html_block_2').style.display = 'none'; document.getElementById('html_block_3').style.display = 'none'; document.getElementById('html_block_4').style.display = 'none'; } if (document.getElementById('dynamic_select').value == 'opt2') { document.getElementById('html_block_2').style.display = 'block'; document.getElementById('html_block_1').style.display = 'none'; document.getElementById('html_block_3').style.display = 'none'; document.getElementById('html_block_4').style.display = 'none'; } if (document.getElementById('dynamic_select').value == 'opt3') { document.getElementById('html_block_3').style.display = 'block'; document.getElementById('html_block_1').style.display = 'none'; document.getElementById('html_block_2').style.display = 'none'; document.getElementById('html_block_4').style.display = 'none'; } if (document.getElementById('dynamic_select').value == 'opt4') { document.getElementById('html_block_4').style.display = 'block'; document.getElementById('html_block_1').style.display = 'none'; document.getElementById('html_block_2').style.display = 'none'; document.getElementById('html_block_3').style.display = 'none'; } } 
 <select id="dynamic_select"> <option value="opt1">Display html block 1</option> <option value="opt2">Display html block 2</option> <option value="opt3">Display html block 3</option> <option value="opt4">Display html block 4</option> </select> <button onClick="filter()">Filter</button> <div id="html_block_1">Block 1</div> <div id="html_block_2">Block 2</div> <div id="html_block_3">Block 3</div> <div id="html_block_4">Block 4</div> 

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

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