doujiu6976 2017-12-26 11:31
浏览 73
已采纳

java dropdown计算出的数字发布到下一页

I have a form with dropdown select, each selection adds the value to calculate a total. I did so with the script below:

$(function(){
    $("select.calculate").on("change", calc);
    $("input[type=checkbox].calculate").on("click", calc);
    function calc() {
        var basePrice = 60;
        newPrice = basePrice;
        $("select.calculate option:selected, input[type=checkbox].calculate:checked").each(function () {
            newPrice += parseInt($(this).data('price'), 10);
        });

        newPrice = newPrice.toFixed(2);
        $("#item-price").html(newPrice);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="test" input name="test" class="calculate">
    <option data-price="0" value=" ">0</option>
    <option data-price="70" value="Test x 1">1</option>
    <option data-price="140" value="Test x 2">2</option>
    <option data-price="210" value="Test x 3">3</option>
</select>

<span id="item-price">0</span>

It works great on the page, but I want to carry the total number forward to the next page, like how I would carry the chosen choices. I am using the POST method, so far the next page is able to get the choices I made via POST.

But I can't seem to figure out how I would go about getting the total number to be carried forward to next page.

Any ideas?

</div>
  • 写回答

1条回答 默认 最新

  • duandu1915 2017-12-26 11:38
    关注

    you need to save the value in hidden field and then post to the next page just like below

    <input type='hidden' name='item_price' value='' id='item_price_val'>
    

    change your jquery function with

    $(function(){
    $("select.calculate").on("change", calc);
    $("input[type=checkbox].calculate").on("click", calc);
        function calc() {
            var basePrice = 60;
            newPrice = basePrice;
            $("select.calculate option:selected, input[type=checkbox].calculate:checked").each(function () {
                newPrice += parseInt($(this).data('price'), 10);
            });
    
            newPrice = newPrice.toFixed(2);
            $("#item-price").html(newPrice);
            $("#item_price_val").val(newPrice);
    
        }
    });
    

    and on PHP page get the value from the input variable like

    $item_price = $_POST['item_price'];
    

    I hope it will works for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题