PHP如果复选框选中显示列表


PHP if Checkbox checked show list

我想显示一个基于一些php参数的列表。

不让这两个选项默认出现(因为它们在帖子上启用了存款参数),我希望默认启用全额支付选项,并且付款计划选项只在选中复选框后出现。

我已经尝试了一百万种不同的方法,但我对php的经验相当有限。

下面是代码:
<div class="wc-deposits-wrapper <?php echo WC_Deposits_Product_Manager::deposits_forced( $post->ID ) ? 'wc-deposits-forced' : 'wc-deposits-optional'; ?>">
<?php if ( ! WC_Deposits_Product_Manager::deposits_forced( $post->ID ) ) : ?>
    <ul class="wc-deposits-option">
        <li><input type="radio" name="wc_deposit_option" value="yes" id="wc-option-pay-deposit" /><label for="wc-option-pay-deposit">Pay Deposit</label></li>
        <li><input type="radio" name="wc_deposit_option" value="no" id="wc-option-pay-full" /><label for="wc-option-pay-full">Pay in Full</label></li>
    </ul>
<?php endif; ?>

这是我对我想要的东西的可怕尝试,也许你能破译它!

<div class="wc-deposits-wrapper <?php echo WC_Deposits_Product_Manager::deposits_forced( $post->ID ) ? 'wc-deposits-forced' : 'wc-deposits-optional'; ?>">
<?php if ( ! WC_Deposits_Product_Manager::deposits_forced( $post->ID ) ) : ?>
    <ul class="wc-deposits-option">
    <li><input type="radio" name="wc_deposit_option" value="no" id="wc-option-pay-full" /><label for="wc-option-pay-full">Pay in Full</label></li>
    </ul>
    <?php endif; ?>
<input type="checkbox" name="plans" value="yes">    
<?php if ( isset($_POST['plans']) ) : ?>
    <ul class="wc-deposits-option">
        <li><input type="radio" name="wc_deposit_option" value="yes" id="wc-option-pay-deposit" /><label for="wc-option-pay-deposit">Pay Deposit</label></li>
    </ul>
<?php endif; ?>

提前感谢在这方面的任何帮助!

我建议使用jQuery而不是纯PHP解决方案。如果您需要PHP解决方案,这是可能的,但正如其他评论中提到的,它需要重新加载页面。

在jQuery中是这样实现的:

如果你给你的复选框一个ID,比如:<input type="checkbox" id="plans-checkbox" name="plans" value="yes">

为字段指定一个ID,只希望在选中plans框时显示这个ID(只有一个,因为ID只能给出一次),如下所示:

<ul class="wc-deposits-option" id="pay-deposit-ul">

然后你可以使用一些像这样的jQuery来切换选项,而不必重新加载页面。

// Hide the pay deposit ul by default (you could also handle this with CSS by giving display:none; to the ID)
$("#pay-deposit-ul").hide();
$("#plans-checkbox").change(function() {
    if ($(this).checked) {
        // Plans checkbox is selected
        $("#pay-deposit-ul").show(); // #pay-deposit-ul means jQuery selects the ID of the name pay-deposit-ul
    }
    else
    {
        // Plans checkbox is unselected
        $("#pay-deposit-ul").hide();
    }
 });

我希望这对你有帮助。如果您以前从未使用过jQuery,那么不必担心。网上有很多基本的资源(比如http://andreehansson.se/the-basics-of-jquery/),你会发现与用户界面的交互真的很方便,而且节省了很多时间。