javascript 使用javascript显示表单元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14681660/
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
show form elements using javascript
提问by Pradip Borde
Can anyone please help me through this.... I have two input fields in my form . One of which is hidden and I want to show it using a button. Now when i tried to show it using onClick() function its not responding...
任何人都可以帮我解决这个问题.... 我的表单中有两个输入字段。其中之一是隐藏的,我想使用按钮显示它。现在,当我尝试使用 onClick() 函数显示它时,它没有响应...
can anyone give me code snippet to do so....
任何人都可以给我代码片段这样做....
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function show()
{
document.getElementById('passwd').style.display="block" ;
}
</script>
</head>
<body>
<form action="demo.html" >
<input type="text" name="user" />
<br />
<input type="text" id="passwd" name="password" style="display:none;" />
<input type="button" onClick="show()" name="show" value="show" />
<br />
<input type="submit" value="OK" />
</form>
</body>
</html>
plz help
请帮忙
回答by Ja?ck
This is because of your <input>
declaration:
这是因为您的<input>
声明:
<input type="button" onClick="show()" name="show" value="show" />
When you call show()
JavaScript will attempt to resolve the symbol show
first; because you're calling show()
from inlined code, the resolution takes place in document
, which attempts to resolve the symbol based on the name
or id
attribute of your input box.
当您调用show()
JavaScript 时,将首先尝试解析符号show
;因为您是show()
从内联代码调用的,所以解析发生在 中document
,它尝试根据输入框的name
或id
属性解析符号。
Solutions
解决方案
Rename the button:
重命名按钮:
<input type="button" onClick="show()" name="showbutton" value="show" />
Rename the function:
重命名函数:
function showPasswordInputBox()
{
// your code here
}
<input type="button" onClick="showPasswordInputBox()" name="show" value="show" />
Don't use in-line code:
不要使用内联代码:
function show()
{
// whatever
}
var showButton = document.getElementsByName('show')[0];
showButton.addEventListener('click', show, false);
See also
也可以看看
Don't give event handler the same name as a field!
回答by polin
There is a problem with naming convention of show() function in javascript. Because just change the name to show1(). it'll work
javascript 中 show() 函数的命名约定存在问题。因为只需将名称更改为 show1()。它会工作
function show1()
{
alert("ok");
}
<input type="button" name="show" value="show" onclick="show1()" />
回答by Ulug'bek Ro'zimboyev
you can use jqueryand write following code: $("#passwd").show()
or you can use addClass('here_css_class')
and css code .here_css_class{
display: block
}
您可以使用jquery并编写以下代码:$("#passwd").show()
或者您可以使用addClass('here_css_class')
和 css 代码.here_css_class{
display: block
}
回答by Sagar Hirapara
try this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function show()
{
document.getElementById('passwd').style.display="block" ;
}
</script>
</head>
<body>
<form action="demo.html" >
<input type="text" name="user" />
<br />
<input type="text" id="passwd" name="password" style="display:none;" />
**<button onClick="show()" >Show</button>**
<br />
<input type="submit" value="OK" />
</form>
</body>
</html>
回答by Riju Mahna
You need to use this:
你需要使用这个:
onClick="javascript:show();"
or simply
或者干脆
onClick="show();"
Basically, just add the semi-colon :)
基本上,只需添加分号:)