2016-07-08 46 views
1
<!DOCTYPE html> 
<html> 
<head> 
<style> 
button.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; 
} 

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

button.accordion:after { 
    content: '\02795'; 
    font-size: 13px; 
    color: #777; 
    float: right; 
    margin-left: 5px; 
} 

button.accordion.active:after { 
    content: "\2796"; 
} 

div.panel { 
    padding: 0 18px; 
    background-color: white; 
    max-height: 0; 
    overflow: hidden; 
    transition: 0.6s ease-in-out; 
    opacity: 0; 
} 

div.panel.show { 
    opacity: 1; 
    max-height: 500px; 
} 
</style> 
</head> 
<body> 

<form action="page2.html"> 

<button class="accordion">Section 1</button> 
<div class="panel"> 
    Ch.1: <input type=checkbox id="ch1" ><br> 
    Ch.1: <input type=checkbox id="ch1"><br> 
    Ch.1: <input type=checkbox id="ch1"><br> 
</div> 

<button class="accordion">Section 2</button> 
<div class="panel"> 
    Ch.1: <input type="checkbox" id="ch1"><br> 
    Ch.1: <input type="checkbox" id="ch1"><br> 
    Ch.1: <input type="checkbox" id="ch1"><br> 
</div> 

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

for (i = 0; i < acc.length; i++) { 
    acc[i].onclick = function(){ 
     this.classList.toggle("active"); 
     this.nextElementSibling.classList.toggle("show"); 
    } 
} 
</script> 

</body> 
</html> 

當我嘗試點擊「第1部分」或「第2部分」時,頁面會自動重定向到page2.html。這是爲什麼發生? 並且使用相同的代碼,我應該如何在「onsubmit」上添加一個java腳本函數?同類型爲什麼html中的表單動作會自動觸發?

+0

添加'的onsubmit = 「返回false;」'形式標籤 – Viney

+0

但IDK的原因 – Viney

回答

0

您需要使用type="button"的按鈕標記,以便不被視爲按鈕提交引起形式的行動執行

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

 
for (i = 0; i < acc.length; i++) { 
 
    acc[i].onclick = function(){ 
 
     this.classList.toggle("active"); 
 
     this.nextElementSibling.classList.toggle("show"); 
 
    } 
 
}
button.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; 
 
} 
 

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

 
button.accordion:after { 
 
    content: '\02795'; 
 
    font-size: 13px; 
 
    color: #777; 
 
    float: right; 
 
    margin-left: 5px; 
 
} 
 

 
button.accordion.active:after { 
 
    content: "\2796"; 
 
} 
 

 
div.panel { 
 
    padding: 0 18px; 
 
    background-color: white; 
 
    max-height: 0; 
 
    overflow: hidden; 
 
    transition: 0.6s ease-in-out; 
 
    opacity: 0; 
 
} 
 

 
div.panel.show { 
 
    opacity: 1; 
 
    max-height: 500px; 
 
}
<form action="page2.html"> 
 

 
<button type="button" class="accordion">Section 1</button> 
 
<div class="panel"> 
 
    Ch.1: <input type=checkbox id="ch1" ><br> 
 
    Ch.1: <input type=checkbox id="ch1"><br> 
 
    Ch.1: <input type=checkbox id="ch1"><br> 
 
</div> 
 

 
<button type="button" class="accordion">Section 2</button> 
 
<div class="panel"> 
 
    Ch.1: <input type="checkbox" id="ch1"><br> 
 
    Ch.1: <input type="checkbox" id="ch1"><br> 
 
    Ch.1: <input type="checkbox" id="ch1"><br> 
 
</div> 
 
</form>

0

你必須提到行動如果您執行的任何操作將重定向到您爲操作提供的頁面,則其他明智的按鈕屬性。

你可以嘗試像這樣onsubmit。

 <form onsubmit="return yourFunctionNmae()"> 
      <input type="submit" value="Submit"/> 
    </form> 

的Javascript:

 <script type="html/javascript"> 
      function yourFunctionName() 
      { 
       //required action to be done here. 
      } 
    </script> 
相關問題