admin管理员组

文章数量:1304020

So disclaimer, I already found the answer to this but I'm posting it as a question in case it helps somebody else.

Scenario: Inside a form in the page you have a couple of accordions so that the user can collapse the parts they already looked at. If you follow most examples expanding the accordions will actually auto submit the form, no bueno.

<!DOCTYPE html>
<html>
<head>
<style>
.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
}

.active, .accordion:hover {
    background-color: #ccc; 
}

.panel {
    padding: 0 18px;
    display: none;
    background-color: white;
    overflow: hidden;
}
</style>
</head>
<body>

<h2>Accordion</h2>

<form action="fail" method="post" enctype="multipart/form-data">

<button class="accordion">Section 1</button>
<div class="panel">
  <input type="checkbox" name="checkbox1.1">Checkbox 1.1
  <br>
  <input type="checkbox" name="checkbox1.2">Checkbox 1.2
</div>

<button class="accordion">Section 2</button>
<div class="panel">
  <input type="checkbox" name="checkbox2.1">Checkbox 1.1
  <br>
  <input type="checkbox" name="checkbox2.2">Checkbox 1.2
</div>
<input type="submit" value="Submit">
</form>

<script>
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
    acc[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var panel = this.nextElementSibling;
        if (panel.style.display === "block") {
            panel.style.display = "none";
        } else {
            panel.style.display = "block";
        }
    });
}
</script>

</body>
</html>

So disclaimer, I already found the answer to this but I'm posting it as a question in case it helps somebody else.

Scenario: Inside a form in the page you have a couple of accordions so that the user can collapse the parts they already looked at. If you follow most examples expanding the accordions will actually auto submit the form, no bueno.

<!DOCTYPE html>
<html>
<head>
<style>
.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
}

.active, .accordion:hover {
    background-color: #ccc; 
}

.panel {
    padding: 0 18px;
    display: none;
    background-color: white;
    overflow: hidden;
}
</style>
</head>
<body>

<h2>Accordion</h2>

<form action="fail" method="post" enctype="multipart/form-data">

<button class="accordion">Section 1</button>
<div class="panel">
  <input type="checkbox" name="checkbox1.1">Checkbox 1.1
  <br>
  <input type="checkbox" name="checkbox1.2">Checkbox 1.2
</div>

<button class="accordion">Section 2</button>
<div class="panel">
  <input type="checkbox" name="checkbox2.1">Checkbox 1.1
  <br>
  <input type="checkbox" name="checkbox2.2">Checkbox 1.2
</div>
<input type="submit" value="Submit">
</form>

<script>
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
    acc[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var panel = this.nextElementSibling;
        if (panel.style.display === "block") {
            panel.style.display = "none";
        } else {
            panel.style.display = "block";
        }
    });
}
</script>

</body>
</html>
Share Improve this question asked May 17, 2018 at 20:54 Amos BakerAmos Baker 8397 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Essentially the buttons for the accordion are being treated as submit buttons even though we already have a submit button. In order to override this behavior simply declare the button type as button:

<button type="button" class="accordion">Section 1</button>
<button type="button" class="accordion">Section 2</button>

This may be obvious to some but given that I rarely deal with web stuff this was a very frustrating side affect to find and work around so I hope it helps somebody else.

本文标签: javascriptAccordion inside a form auto submitsStack Overflow