html - Can't put a form in multiple divs
问题描述
I have an accordion menu on my page which spans across multiple divs, and I tried to put it in a form like below
(Note that the code within the divs is very long, so I removed it. It works perfectly fine without the form element.) If I put everything inside a form, nothing seems to display properly anymore, and if it helps, here is my CSS.
.accordion {
background-color: whitesmoke;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
margin-left: 0;
margin-bottom: 0;
margin-top: 0;
border-radius: 0px;
border-bottom: 2px solid #e8e8e8;
}
.active,
.accordion:hover {
background-color: #ccc;
box-shadow: none;
}
.panel {
padding: 0 18px;
background-color: #e8e8e8;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
font-family: Montserrat;
}
<form method="post">
<button class="accordion"></button>
<div class="panel">
<input class="input name" type="text" name="name1" placeholder="name">
<input class="input name" type="text" name="name2" placeholder="last name">
<select class="drop">
<option selected hidden>choose</option>
<option></option>
<option></option>
</select>
<select class="drop">
<option selected hidden>choose</option>
<option></option>
<option></option>
</select>
<br>
<input class="input" type="text" placeholder="date">
<input class="input name" type="tel" pattern="0[0-9]{9}" placeholder="phone number">
<div class="checkbox">
<input type="checkbox" id="current">
<label for="current">currently active</label>
</div>
</div>
<button class="accordion"></button>
<div class="panel">
<input class="input" type="text" placeholder="birth code">
<input class="input name" type="text" placeholder="county">
<br>
<input class="input name" type="text" placeholder="city">
<input class="input" type="text" placeholder="address">
<input class="input name" type="text" placeholder="birthplace">
</div>
<button class="accordion"></button>
<div class="panel">
<input class="input name" type="text" placeholder="bank name">
<input class="input" type="text" placeholder="account code">
<input class="input name" type="text" placeholder="account name">
</div>
<button class="accordion"></button>
<div class="panel">
<input class="input" type="text" placeholder="id code">
<input class="input name" type="text" placeholder="id given on">
<input class="input name" type="text" placeholder="id given by">
<br>
<input class="input name" type="text" placeholder="hire date">
<input class="input" type="text" placeholder="contract number">
</div>
<button class="accordion"></button>
<div class="panel">
<p>Placeholder Text</p>
</div>
<input class="button" type="submit">
</form>
解决方案
<button>
elements have a type
attribute. The default (what you get if, as you do, omit it) is type="submit"
.
When a <button type="submit">
inside a <form>
is clicked, the form gets submitted. This leads to a full page (re-)load, which destroys any Javascript state the page had before the click.
To fix your problem, use the correct button type, which for you is <button type="button">
.
推荐阅读
- android - HttpsURLConnection 错误:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚
- r - 使用 R 的时间序列中的间隙大小计算
- swift - 如何用异步任务装饰午睡请求
- loops - 在 SAS 中循环直到特定关键字
- php - 如何使用 php 在 html 中访问选择列表的值
- c# - 在 C# 中创建自定义事件
- java - Android:我的 listView onClick 事件未按预期工作(顺序混乱)
- powershell - 在Powershell中根据文件中的第一个单词重命名文本文件
- php - php soap - 400 错误请求
- r - 网页抓取:Set_values 和爬虫的问题