0

我在表单提交按钮上使用了 onclick 功能来隐藏 div。但它并没有隐藏。做这个的最好方式是什么?提交表单后,我想在我的页面上隐藏一个 div。

<form action="" method="POST">
<input type="submit" onclick="hide_group_posts();">
</form>

<div id='div_i_want_to_hide'>
<?php include $_SERVER['DOCUMENT_ROOT']."page.php";?>
</div>

<script>
function hide_group_posts(){
$('#div_i_want_to_hide').hide();
}
</script>
4

1 回答 1

9
$('form').submit(function(){
    $('#div_i_want_to_hide').hide();  
});

如果它对您没有帮助,则必须满足以下条件之一:

  • 您没有引用 jQuery 库。
  • 您没有使用 DOM 就绪事件包装代码。
  • 你有错别字。
  • 提交功能起作用了,你得到了新页面,因为你没有阻止默认值。

像这样防止它:

$('form').submit(function(e){
    $('#div_i_want_to_hide').hide();  
    e.preventDefault();
    // Or with: return false;        
});
于 2012-05-06T00:45:10.880 回答