Php 显示循环的最后一个元素时,显示循环外部的另一个元素
我有一个php foreach循环,其中包含以下内容:Php 显示循环的最后一个元素时,显示循环外部的另一个元素,php,jquery,html,Php,Jquery,Html,我有一个php foreach循环,其中包含以下内容: <div class="form-row">...</div> 我想做如下事情: $('.form-row:last'){ $('button.test').show(); } 例如,当它位于表单行的最后一个元素上时,我希望它显示按钮 我该怎么做 如果它在表单行中循环中,那么我将只使用$('button.test').last().show()但由于不在这一范围之内,我不知道该怎么办 这是我
<div class="form-row">...</div>
我想做如下事情:
$('.form-row:last'){
$('button.test').show();
}
例如,当它位于表单行的最后一个元素上时,我希望它显示按钮
我该怎么做
如果它在表单行中
循环中,那么我将只使用$('button.test').last().show()代码>但由于不在这一范围之内,我不知道该怎么办
这是我的完整代码:
<?php global $current_user;
get_currentuserinfo();
$user = $current_user->user_login; ?>
<form class="form" method="POST" action="<?php bloginfo('stylesheet_directory'); ?>/training-insert.php">
<input type="hidden" value="<?php echo $user; ?>" name="test-user" />
<input type="hidden" value="<?php echo the_title(); ?>" name="test-name" />
<?php $counter = 1; if(get_field('step_by_step_training')): ?>
<?php while(the_repeater_field('step_by_step_training')): ?>
<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>
<div class="inner"></div>
<button class="next"></button>
<button class="end"></button>
<div class="clear"></div>
</div>
<div style="margin-bottom:5px;" id="modules-bottom"></div>
</div>
</div>
<?php $counter++; endwhile; ?>
<?php endif; ?>
</form>
<?php if (get_field('test') != "") { ?>
<form class="form" method="POST" action="<?php bloginfo('stylesheet_directory'); ?>/training-insert-alt.php">
<input type="hidden" value="<?php echo $user; ?>" name="test-user" />
<input type="hidden" value="<?php echo the_title(); ?>" name="test-name" />
<input type="hidden" value="<?php echo the_field('test'); ?>" name="test-link" />
<button class="test"></button>
</form>
<?php } ?>
<div class="clear"></div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
<?php get_footer(); ?>
<script type="text/javascript">
jQuery(function($) {
$(document).ready(function() {
// prepend a 'previous' button to all form-rows except the first
$('<button>').addClass('previous').appendTo($('.inner').not(':first'));
// hide all form-rows, but not the first one
$('.form-row').not(':first').hide();
// hide on last step
$('button.next').last().hide();
$('button.end').last().show();
if($('.form-row:last').is(':visible')) {
$('button.test').show();
}
// 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).parents('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).parents('div.form-row').hide().next('div.form-row').show();
});
});
});
</script>
将其包装在函数中,并在每次显示下一行时调用它。或者将其直接包含到显示下一行的零件中:
$('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).parents('div.form-row').hide().next('div.form-row').show();
if($('.form-row:last').is(':visible'))
$('button.test').show();
});
如果要在按下“上一步”按钮时隐藏该按钮:
$('.form-row').on('click', 'button.previous', function(e) {
e.preventDefault();
$(this).parents('div.form-row').hide().prev('div.form-row').show();
$('button.test').hide();
});
我用这个让它工作:
$('.form-row').last().append($('button.test').show());
也许是这个@GoE按钮在循环之外,所以使用last似乎与我尝试的不一样。你能发布你的循环吗?那么我们可以更好地帮助您为什么不能使用PHP呢?
$('.form-row').on('click', 'button.previous', function(e) {
e.preventDefault();
$(this).parents('div.form-row').hide().prev('div.form-row').show();
$('button.test').hide();
});
$('.form-row').last().append($('button.test').show());