javascript - 如何消除输入标签之间的差距

标签 javascript html css

我需要根据用户 radio 标签显示额外的输入标签。我在这里粘贴了工作代码。我想显示配偶的名字和姓氏输入标签。如果不是,则必须显示父亲和母亲的名字和姓氏。但问题是,我看到两者之间存在差距,这是我不想要的。有帮助吗?

<!DOCTYPE html>
<html>
  <head>
  <script>
    function check() {
      if (document.getElementById('Married').checked) 
      {
        document.getElementById('ifYes').style.visibility = 'visible';
      }
      else
        document.getElementById('ifYes').style.visibility = 'hidden';
    }
  </script>
  <form>
    First Name <input type="text" id="UserFirstName" value="Your First Name"> 
    Last Name <input type="text" id="UserLastName" value="Your Last Name"></br></br>
    Marital Status <input type="radio" id="Married" name="MarStatus" onclick="javascript:check();" >Married <input type="radio" id="UnMarried" name="MarStatus" onclick="javascript:check();">UnMarried </br></br>

     <div id="ifYes" style="visibility:hidden">
    Spouse First Name <input type="text" id="SpouseFirstName" value="Spouse First Name">   
    Last Name <input type="text" id="SpouseLastName" value="Spouse Last Name"></br></br>
    </div>
    Father First Name <input type="text" id="FatherFirstName" value="Father First Name"> Last Name <input type="text" id="FatherLastName" value="Father Last Name"></br></br>
    Mother First Name <input type="text" id="FatherFirstName" value="Mother First Name"> Last Name <input type="text" id="FatherLastName" value="Mother Last Name"></br>
    </br>
    <button onclick="window.location.href='b.php'">Submit</button>
  </form>
  </body>
</html>

最佳答案

visibility:hidden will keep the element in the page and occupies that space but does not show to the user.

display:none will not be available in the page and does not occupy any space.

你应该使用 display:none .

你也在使用 </br>错了,应该是<br/>或者简单地 <br> .虽然我建议你不要使用它。相反,您可以使用 div 包装相关信息元素并使用 css 设置样式(margin 属性)。

另一个问题是您应该在单选按钮中使用 value 属性,您可以根据该属性在函数内设置样式。

尝试以下方式:

<script>
  function check(el) {
    if (el.value == "Married") {
      document.getElementById('ifYes').style.display = 'block';
    }
    else
      document.getElementById('ifYes').style.display = 'none';
  }
</script>
<form>
  <div style="margin-bottom:20px;">
    First Name <input type="text" id="UserFirstName" value="Your First Name"> 
    Last Name <input type="text" id="UserLastName" value="Your Last Name">
  </div>
  <div style="margin-bottom:20px;">
    Marital Status <input type="radio" value="Married" name="MarStatus" onclick="javascript:check(this);" >Married <input type="radio" value="UnMarried" name="MarStatus" onclick="javascript:check(this);">UnMarried 
  </div>

  <div id="ifYes" style="display:none; margin-bottom:20px;">
    Spouse First Name <input type="text" id="SpouseFirstName" value="Spouse First Name">   
    Last Name <input type="text" id="SpouseLastName" value="Spouse Last Name">
  </div>
  <div style="margin-bottom:20px;">
    Father First Name <input type="text" id="FatherFirstName" value="Father First Name"> Last Name <input type="text" id="FatherLastName" value="Father Last Name">
  </div>
  <div style="margin-bottom:20px;">
    Mother First Name <input type="text" id="FatherFirstName" value="Mother First Name"> Last Name <input type="text" id="FatherLastName" value="Mother Last Name">
  </div>
  <button onclick="window.location.href='b.php'">Submit</button>
</form>

关于javascript - 如何消除输入标签之间的差距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59850925/

相关文章:

html - 使 CSS Grid 适合最小空间

iphone - iPhone 3g 上的 Safari 按钮的固定位置

javascript - Ember : filter model by text field

javascript - 如何确定 javascript 中引用了哪个函数?

javascript - 此 jQuery 代码无法正常工作的新手问题

javascript - 在函数中添加两个参数到 "then"Protractor

javascript - 如何在html中输出javascript变量的值?

html - 如何仅在第一次浏览网站时加载特定的 set css 命令?

html - css 背景图像显示在我的其他代码前面

html - 我如何让我的 'See more' ("seemorelink") 链接到更靠近我的图像的位置?