HTML表单元素隐藏的onload javascript

HTML form element hidden onload javascript

本文关键字:onload javascript 隐藏 表单 元素 HTML      更新时间:2023-09-26

尝试在加载时隐藏HTML文本输入,但如果选中单选按钮,则表单将显示

http://pastie.org/private/ah39ul5h4jtmlntkbmmda

我已经得到它的工作,我只需要字段隐藏在页面加载。

<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>

我给无线电和字段一个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

<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>

你可以用CSS样式。默认情况下,这将隐藏所有输入:

input {
    visibility: hidden;
}

输入元素没有onload事件,你可以默认使用CSS隐藏元素,然后在单选框被点击时处理它。

另一个选择是对body使用onload。

<body onload="document.forms[0].otheremail.style.visibility='hidden';" >