javascript 选择/取消选择特定下拉选项时切换隐藏的 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5950959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:55:09  来源:igfitidea点击:

Toggle a hidden div when a particular dropdown option is selected/de-selected

javascriptjquery

提问by jwBurnside

Hey everyone, I had a quick question that should be easy for someone who is experienced in Jquery and Javascript.

大家好,我有一个简单的问题,对于在 Jquery 和 Javascript 方面有经验的人来说应该很容易。

I'd like to have a hidden div that is below a drop-down field in my form. When one particular option is selected, the hidden div is displayed. I'd also like it to disappear when any other option is selected. I'm using this as a warning for the user since that option will run some specific db related functions.

我想在表单中的下拉字段下方有一个隐藏的 div。选择一个特定选项时,将显示隐藏的div。我还希望它在选择任何其他选项时消失。我将此用作对用户的警告,因为该选项将运行一些特定的数据库相关功能。

Can anyone give me some hints on how to get this started?

谁能给我一些有关如何开始的提示?

Thanks.

谢谢。

回答by DanielB

Here you go. (online demo http://jsfiddle.net/wwTxw/)

干得好。(在线演示http://jsfiddle.net/wwTxw/

HTML:

HTML:

<select id="select">
    <option value="1">111</option>
    <option value="2">222</option>
    <option value="3">333</option>
</select>

<div id="warning" style="display:none">NO!!!</div>

jQuery Code;

jQuery 代码;

$(function() {
    $('#select').change(function(){
        if ($(this).val() == "2") {
            $('#warning').show();
        } else {
            $('#warning').hide();
        }
    });
});