HTML表单元素隐藏onload javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15721466/
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
HTML form element hidden onload javascript
提问by
Trying to get a HTML text input to be hidden on load, but then if a radio button is checked then the form will show.
试图在加载时隐藏 HTML 文本输入,但如果选中单选按钮,则表单将显示。
http://pastie.org/private/ah39ul5h4jtmlntkbmmda
http://pastie.org/private/ah39ul5h4jtmlntkbmmda
I've got it working sort of, I just need the field to be hidden on page load.
我已经让它工作了,我只需要在页面加载时隐藏该字段。
<form action="profile_edit_gravatar.php" method="post">
<label class="radio inline">
<input type="radio" <?php if($row[16]=='account') {echo ' checked';}?> onload="this.form.otheremail.style.visibility='hidden';" onclick="
if (this.checked) {
this.form.otheremail.style.visibility='hidden';
} else {
this.form.otheremail.style.visibility='visible';
}
return true;
">
Use <?php echo $row[3];?>
</label>
<input type="text" name="otheremail">
</form>
采纳答案by mplungjan
How about I give the radio and field an ID - actually less important now I wrote a script that use getElementsByName...
我给收音机和外地一个 ID 怎么样——现在我写了一个使用 getElementsByName 的脚本实际上不那么重要了...
<?php $hide = $row[16]=='account'; ?>
<style>
<?php if ($hide) { echo '#otheremail {visibility:hidden;}'; ?>
</style>
<script>
window.onload=function() {
var rads = document.getElementsByName("gravatarradios");
rads[0].onclick=rads[1].onclick=function() {
this.form.otheremail.style.visibility=(this.value=="Use")?'hidden':'visible';
}
if (<?php echo $hide; ?>) rads[0].click(); else rads[1].click();
}
</script>
HTML
HTML
<form action="profile_edit_gravatar.php" method="post">
<label class="radio" for="gravatarradios1">
<input type="radio" name="gravatarradios" id="gravatarradios1" value="Use">
Use </label>
<label class="radio" for="gravatarradios2">
<input type="radio" name="gravatarradios" id="gravatarradios2" value="Use other">
Use another email:
</label>
<input type="email" name="otheremail" id="otheremail" placeholder="Gravatar email address">
</form>
回答by mplungjan
There is no onload event for input element, you can hide the element by default using CSS then work on it when the radio box clicked.
input 元素没有 onload 事件,您可以默认使用 CSS 隐藏该元素,然后在单击单选框时对其进行处理。
Another option is to use onload for body.
另一种选择是对 body 使用 onload。
<body onload="document.forms[0].otheremail.style.visibility='hidden';" >
回答by Fleming Slone
You can style it with CSS. This would hide all inputs by default:
您可以使用 CSS 设置样式。默认情况下,这将隐藏所有输入:
input {
visibility: hidden;
}