Javascript 如何在HTML表单中指定按钮的行为?
我的页面是这样编码的,我所有的东西都在一个表单中Javascript 如何在HTML表单中指定按钮的行为?,javascript,html,Javascript,Html,我的页面是这样编码的,我所有的东西都在一个表单中 <form onsubmit='return validate()' action='#' method='post'> <button class='accordian'>Drop Stuff1</button> <div class='panel'> //stuff here </div> <div
<form onsubmit='return validate()' action='#' method='post'>
<button class='accordian'>Drop Stuff1</button>
<div class='panel'>
//stuff here
</div>
<div>
//stuff here too
</div>
<button class='accordian'>Drop Stuff2</button>
<div class='panel'>
//and here
</div>
<div>
//and here too
</div>
<input type='submit' value='Submit!'>
</form>
放下东西1
//这里的东西
//这里也有东西
放下东西2
//这里呢
//这里也是
问题是,当我单击手风琴的任何按钮时,它会运行我的validate()
函数。如何设置它,使窗体的onsubmit='return validate()
部分仅在调用时调用。将其改为按钮
,它将不会提交表单并调用您的函数
例如,当单击类型为“提交”的元素时,会触发“提交”事件。因此,只需将
type=“button”
添加到其他按钮,因为默认值是submit
,当然,另一个选项是将对validate函数的调用从
标记移动到
标记
<form action='#' method='post'>
<button class='accordian'>Drop Stuff1</button>
<div class='panel'>
//stuff here
</div>
<div>
//stuff here too
</div>
<button class='accordian'>Drop Stuff2</button>
<div class='panel'>
//and here
</div>
<div>
//and here too
</div>
<input type='submit' value='Submit!' onclick='return validate();'>
</form>
放下东西1
//这里的东西
//这里也有东西
放下东西2
//这里呢
//这里也是
为什么要使用手风琴按钮?像div/span/li这样的东西看起来更容易设计,也更合理。你为什么把这些都用标签包起来?@Adam可以清楚地看到这一点