0

我有一个在 PHP 中名为“formscontainer”的 div 中重复多次的表单,而我想让一个表单提交,它们各自的 div 消失了效果隐藏(“慢”)

这里是示例代码

<script type="text/javascript">
 $(function() {
      $("form").each(function() {
          $(this).validate( {
              submitHandler: function(formbeingsubmitted) {
                  $.post('process.php', $(formbeingsubmitted).serialize(), function(data) {
                      $('#results').html(data);
                      $ (formbeingsubmitted).hide("slow");
                  });
              }
          });
      });
  });
</script>
</head>
<body>
<?php do { ?>
<div id="formscontainer">
  <form method="post" id="form1">
    <label for="name" id="name_label">form </label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>

    <input type="submit" name="submit" value="SEND"> 
  </form>
</div>
<div id="formscontainer">
  <form method="post" id="form1">
    <label for="name" id="name_label">form </label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>

    <input type="submit" name="submit" value="SEND"> 
  </form>
</div>
<div id="formscontainer">
  <form method="post" id="form1">
    <label for="name" id="name_label">form </label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>

    <input type="submit" name="submit" value="SEND"> 
  </form>
</div>
<?php } while ($row_var = mysql_fetch_assoc($var)); ?>
<div id="results"></div>
</body>
</html>
4

1 回答 1

0
$(function () {
    $("form").each(function () {
        var par_div = $(this).closest('div'); //finding the closest div
        $(this).validate({
            submitHandler: function (formbeingsubmitted) {
                $.post('process.php', $(formbeingsubmitted).serialize(), function (data) {
                    $('#results').html(data);
                    $(formbeingsubmitted).hide("slow");
                    par_div.hide('slow'); //hide the div
                });
            }
        });
    });
});

你也可以使用

$(this).parents('div');代替$(this).closest('div');

但这$(this).closest('div');更快

于 2013-08-15T16:16:00.580 回答