2014-10-30 170 views
2

以下是我的代码。它由三个按钮和一个显示屏组成。点击显示或隐藏信息

<html> 
<head> 
<script type="text/javascript"> 


</script> 
</head> 
<body> 
<form name="fun" > 
Display Screen<input type="textfield" name="ans" value=""> 
<br> 
<input type="button" value="1" onClick="document.fun.ans.value+='1'"> 
<input type="button" value="hide"> 
<input type="button" value="show"> 


</form> 
</body> 
</html> 

当前当您按1时,它将在显示屏上显示1。 我想实现一个功能,以便如果您单击隐藏,然后按1将不会显示。如果按下显示并按下1,则屏幕上将显示1。

可能的方法是只要禁用1按钮,如果你点击隐藏,但我仍然希望用户能够点击按钮后按隐藏只是不显示任何东西。

我是JS新手,所以请原谅,如果这是一个坏问题。

+0

看看我的答案,我认为这是你在找什么...希望它帮助! – brso05 2014-10-30 18:07:22

+0

你的意思是,如果已经点击了隐藏,它不应该在点击“1”时追加任何内容(如果点击已经点击了,点击“1”后追加1)?或完全隐藏文本字段? – nicael 2014-10-30 18:10:16

回答

1

我包裹所有JS在<script>能够与VARS工作。

<script> 
    var hide = 0; 
    function appendChar(){ 
     if(hide==0){ 
      document.fun.ans.value+='1'; 
     } 
    } 

</script> 

接着我们来修改HTML:

<input type="button" value="1" onClick="appendChar()"> 
<input type="button" value="hide" onclick="hide=1;"> 
<input type="button" value="show" onclick="hide=0;"> 
0

在显示和隐藏使用JavaScript的元素的一个例子:

<html> 
<head> 
<script type="text/javascript"> 


</script> 
</head> 
<body> 
<form name="fun" > 
Display Screen<input id="ans" type="textfield" name="ans" value=""> 
<br> 
<input type="button" value="1" onClick="document.fun.ans.value='1'"> 
<input type="button" value="hide" onclick="hide()"> 
<input type="button" value="show" onclick="show()"> 

<script type="text/javascript"> 
function hide() 
{ 
    document.getElementById("ans").style.display = "none"; 
} 
function show() 
{ 
    document.getElementById("ans").value = ''; 
    document.getElementById("ans").style.display = "inline-block"; 
} 
</script> 


</form> 
</body> 
</html> 
+0

谁低估了这个至少说为什么你downvoted正确的答案??? – brso05 2014-10-31 17:13:43