根据无线电输入状态复制值

时间:2016-11-14 21:11:59

标签: php jquery html wordpress

我正在创建一个WordPress主题,我需要将一些值复制到带有动态填充字段的Gravity Forms页面。我正在加载选项(空调电源)作为ACF中继器现场与该特定单元的价格一起保存。我需要根据检查的无线电将此价格复制到静态div字段加上隐藏的输入字段,以便在表单中使用。

下面是我需要实现的简单结构和我到目前为止的代码。我需要的是将某个地方保存的价格(可以是PHP变量或任何东西)复制到div(可能是jQuery)并同时复制到隐藏字段中,并根据用户的选择动态更改它。

code structure

<form class="order" action="<?php echo get_permalink(26); ?>">
<div>
<?php if($power) { ?>
    <div class="power">
        <?php $power_counter = 1; while(have_rows('power')) { the_row();
            $power= get_sub_field('power');
            $price = get_sub_field('price');
        ?>
            <input type="radio" name="power" id="power<?php echo $power_counter; ?>" value="<?php echo get_the_title() . ' ' . $power; ?>"<?php if($power_counter == 1) { echo "checked"; }; ?>><label for="power<?php echo $power_counter; ?>"><?php echo $power; ?></label>
            <input type="hidden" name="price_wo_vat" id="price<?php echo $power_counter; ?>" value="<?php echo $price; ?>">
            <div id="price<?php echo $power_counter; ?>" class="price"><?php echo $price. __(' CZK','klima'); ?></div>
        <?php $power_counter++; }; ?>
    </div>
<?php }; ?>
<input type="number" name="pcs" value="1" min="1" max="99">
<input type="submit" value="<?php _e('Order','klima'); ?>">
</div>

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
$(document).ready(function() { 
    var selPrice = parseInt($('#selected_price').val()),
        chosenPrice = parseInt($('input[name="power"]:checked').val()),
        qty = parseInt($('input[name="pcs"]').val()),
        initTotal = calcTotal(chosenPrice, qty);
  
    $('#total').text(initTotal);
  
    $('input[name="pcs"]').on('change', function() { 
        var myQty = $(this).val(),
            chooseQty = parseInt($('input[name="power"]:checked').val()),
            newQty = calcTotal(chooseQty, myQty);

        $('#total').text(newQty);
    });
  
    $('input[name="power"]').on('click', function() { 
        var myPrice = $(this).val(),
            myQty = parseInt($('input[name="pcs"]').val()),
            newTotal = calcTotal(myPrice, myQty);
      
        $(this).attr('checked',true);
        $('#total').text(newTotal);
    });
  
});

function calcTotal(price, qty) {
    var total = price * qty;
    return total
}
&#13;
.power {
    margin-bottom: 10px; 
}

.total-container {
    padding: 5px;
    background-color: #DDD;
    width: 50px;
    margin-bottom: 10px;
}

input[name='pcs'] {
    padding: 5px;
    text-align: center;
    width: 40px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="order" action="#">
<div>
    <div class="power">
        <!-- #Power 1 -->
        <input type="radio" name="power" id="power1" value="62" data-xvat="80" checked="true">
        <label for="power1">Option 1 - £62</label>
        <br>
  
        <!-- #Power 2 -->
        <input type="radio" name="power" id="power2" value="114" data-xvat="160">
        <label for="power2">Option 2 - £114</label>
        <br>
      
        <!-- #Power 3 -->
        <input type="radio" name="power" id="power3" value="139" data-xvat="240">
        <label for="power3">Option 3 - £139</label>
        <br>

      
    </div>
    <div class="total-container">£<span id="total"></span></div> 
    <input type="hidden" name="selected_price" id="selected_price" value="0">
    <input type="number" name="pcs" value="1" min="1" max="99">
    <input type="submit" value="Order">
</div>
&#13;
&#13;
&#13;