javascript - 根据文本输入显示/隐藏 div

标签 javascript html css

我想在用户输入“密码”的地方做一些事情,它会根据输入的密码显示一个 div。我看到类似这样的东西可以用数字工作,但我对如何让它用字母工作感到困惑.我怎样才能使这项工作?这是 JSfiddle:http://jsfiddle.net/3XGGn/405/

$(document).ready(function(){
    $("#buttontest").click(function(){
            
        if ($('#full_day').val() == yes) {   
            $("#yes").show("fast"); 
        }
        if ($('#full_day').val() == no) {   
            $("#no").show("fast"); 
        } 
        
        else {
            $("#yes").hide("fast");    
            $("#no").hide("fast");
            $("#incorrect").show("500");
        }
    });
    $("#full_day").change(function() {
        $("#incorrect").hide("500");
    });
                

});
#yes {display:none;}
#no {display:none;}
#incorrect {display:none;}
<p>type "yes" or "no"</p>
<div id="yes">
That's Correct!
</div>
<div id="no">
Also correct!
</div>
<div id="incorrect">
Sorry, Try again!
</div>
<input type='text' id="full_day"/>
<input type='button' id="buttontest" value="clickme"/>

最佳答案

试试这个它会接受任何大小写(小写/大写)的“yes”和“no”字符串,即使在混合大小写的情况下也是如此。

    <!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <meta charset="utf-8" />
    <script>
        $(document).ready(function () {
            $("#buttontest").click(function () {
                $(".all").hide("fast");
                if ($('#full_day').val().toLocaleLowerCase() == "yes") {
                    $("#yes").show("fast");
                } else if ($('#full_day').val().toLocaleLowerCase() == "no") {
                    $("#no").show("fast");
                } else {
                    $("#incorrect").show("500");
                }
            });
            $("#full_day").change(function () {
                $("#incorrect").hide("500");
            });
        });
    </script>
    <style>
        #yes {
            display: none;
        }

        #no {
            display: none;
        }

        #incorrect {
            display: none;
        }
    </style>
</head>
<body>

    <p>type "yes" or "no"</p>
    <div class="all" id="yes">
        That's Correct!
    </div>
    <div class="all" id="no">
        Also correct!
    </div>
    <div class="all" id="incorrect">
        Sorry, Try again!
    </div>
    <input type='text' id="full_day" />
    <input type='button' id="buttontest" value="clickme" />
</body>
</html>

关于javascript - 根据文本输入显示/隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45644871/

相关文章:

javascript - navigator.geolocation.watchPosition 只是不工作

javascript - 使用 Media Source Extension API 发出开始播放高可变比特率视频的问题

javascript - 为什么乘以和除以N "fix"浮点表示?

javascript - AutoFill chrome 扩展不更新 ng-model 值

javascript - 添加类定义的不透明度不会影响 Webkit 中的元素

html - 如何让 w3-cell 不自动调整?

html - MDL 卡未与父卡对齐

javascript - 如何根据 css 属性值过滤 HTML 元素?

html - 如何在表格 td 中将图像居中?

HTML 表格,跟踪标题的水平滚动和垂直滚动 - 文本宽度问题