好的,所以我有一个窗体,它有几组字段,如Facebook帐户设置编辑,以显示您点击它时。Jquery显示和隐藏导致表单提交
<form id="test" action="#" method="POST">
<ul class="items">
<li>
<button class="show-content-button">Show</button>
<div class="form-content ui-helper-hidden">
Hello
</div>
</li>
<li>
<button class="show-content-button">Show</button>
<div class="form-content ui-helper-hidden">
Hello
</div>
</li>
<li>
<button class="show-content-button">Show</button>
<div class="form-content ui-helper-hidden">
Hello
</div>
</li>
</ul>
</form>
<script type="text/javascript">
$(function() {
$("form#test").submit(function() {
alert("hello i have been submitted");
})
$(".show-content-button").click(function() {
var $button = $(this);
if ($button.text() === "Show") {
$(".form-content", $button.parent()).fadeIn(400);
$button.html("Hide");
} else if ($button.text() === "Hide") {
$(".form-content", $button.parent()).fadeOut(200);
$button.html("Show");
}
})
})
</script>
当我点击其中一个节目时,它会导致表单被提交......为什么?我可以通过在按钮单击事件上返回false来解决这个问题,但是,我觉得这是一个黑客而不是解决方案。任何人帮助?
让我试试这个东西 – Michael 2011-05-30 23:07:11
你是对的我的朋友。这是奇怪的默认行为,有点愚蠢。哦,好吧。我喜欢我的按钮方式,因为它使按钮内的HTML样式变得容易。感谢您的正确解决方案! – Michael 2011-05-30 23:16:21