Javascript 隐藏和显示文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14578908/
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-08-24 17:16:45  来源:igfitidea点击:

Hide and show a text field

javascript

提问by DjangoDev

i am a beginer to javascript.I want to show a hidden textbox on a button click.i do the bellow code, but it doesnt work.

我是 javascript 的初学者。我想在单击按钮时显示一个隐藏的文本框。我执行以下代码,但它不起作用。

What is the problem with my code?

我的代码有什么问题?

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
        function display() {
            var z = prompt("enter your name...");
            if(z != null) {
                document.getElementById("demo").innerHTML = "thankyou " + z + "..";
                document.getElementById("case").style.visibility = 'visible';
            } else {
                document.getElementById("demo").innerHTML = "thankyou";
            }
        }
        </script>
        <title></title>
    </head>
    <body>
        <p id="demo">
            click on the button.....
        </p><button type="button" onclick="display()">submit</button>
        <form>
            <input type="text" id="case" name="myText" style="display:none">
        </form>
    </body>
</html>

回答by user2001057

replace

代替

document.getElementById("case").style.visibility='visible';

with

document.getElementById("case").style.display='block';

回答by Selvakumar Ponnusamy

Change the style as display block instead of visibility,

将样式更改为显示块而不是可见性,

document.getElementById("case").style.display='block';

or have your text box as visibility hidden instead of display:none

或将您的文本框作为可见性隐藏而不是显示:无

<input type="text" name=<name> style="visibility:hidden"/>

回答by Vidhee

The following two statements will display the element with id "case":

以下两个语句将显示 id 为“case”的元素:

document.getElementById("case").style.display='block'; 

or

或者

document.getElementById("case").style.display='';

The following statement will hide the element with id "case":

以下语句将隐藏 ID 为“case”的元素:

document.getElementById("case").style.display='none'; 

回答by jaismeensandhu

Display:none works fine with HTML to hide a button

Display:none 适用于 HTML 以隐藏按钮