1

我有一个分步系统,根据您所处的步骤隐藏和显示 div,这里是 jQuery:

<script type="text/javascript">
jQuery(function($) {
$(document).ready(function() {    

    // prepend a 'previous' button to all form-rows except the first
    $('<button>').addClass('previous').appendTo($('.form-row').not(':first'));

    // hide all form-rows, but not the first one
    $('.form-row').not(':first').hide();

    // hide on last step
    $('button.next').last().hide();

    // add the submit button to the last form-row
    $('<input>').addClass('submit').prop('type', 'submit').val('My Dashboard').appendTo($('.form-row:last'));


    // handle the previous button, we need to use 'on' here as the
    // previous buttons don't exist in the dom at page load
    $('.form-row').on('click', 'button.previous', function(e) {
        e.preventDefault();
        $(this).parent('div.form-row').hide().prev('div.form-row').show();
    });

    $('button.next').click(function(e) {
        // prevent the next buttons from submitting the form
        e.preventDefault();
        // hide this form-row, and show the next one
        $(this).parent('div.form-row').hide().next('div.form-row').show();
    });    

});
});
</script>

这是我的html:

<div class="form-row">
  <h2 style="float:left;margin-left:7px;">
    <?php the_title(); ?>
  </h2>
  <h2 style="float:right;"><?php echo $counter; ?> of <?php echo $total; ?></h2>
  <div class="clear"></div>
  <div id="module-area" style="margin-top:0px!IMPORTANT;">
    <div id="modules-top"></div>
    <div id="modules-repeat">
      <p class="training"><?php echo the_sub_field('introduction'); ?></p>
      <?php if(get_sub_field('training_images')): ?>
      <?php while(has_sub_field('training_images')): ?>
      <img class="training" src="<?php echo the_sub_field('image'); ?>" />
      <?php endwhile; ?>
      <?php endif; ?>
      <p class="training"><?php echo the_sub_field('conclusion'); ?></p>
      <button class="next"></button>
      <div class="clear"></div>
    </div>
    <div style="margin-bottom:5px;" id="modules-bottom"></div>
  </div>
</div>

这一直有效,直到我在<div class="form-row">. 我怎样才能让它也隐藏封闭的div?

4

2 回答 2

1

尝试使用parents('div.form-row')而不是parent('div.form-row')

于 2012-09-26T14:44:35.490 回答
1

我认为您正在寻找.closest()方法。

尝试:

$('button.next').click(function(e) {
    e.preventDefault();

    var _parent = $(this).closest('div.form-row');

    _parent.hide();
    _parent.next().show();
});

当涉及到长 jQuery 链接以提高可读性时,我喜欢将事情分解。

于 2012-09-26T14:49:49.163 回答