0

我有一个插件,可以很好地完成他的工作。我只需要一个额外的选项。该表格与总价相呼应。我需要根据下拉菜单中的选择在总价中添加运费。下面是下拉列表,标签中是总价格的输出,具体取决于所选的产品。我希望有人可以提供帮助。

<table>
<select name="shipper">
<option value="25">Netherlands</option>
<option value="49">Europe</option>
</select>

<td><strong>
  <?php 
    echo $this->output_currency(
      $totalPrice + $totalShipping + $shipper, 
      $currencySymbol, 
      $decimalPoint, 
      $thousandsSeperator) 
  ?></strong></td>
</table>

我已经尝试了以下所有解决方案,但没有一个有效。Tim 的版本显示了总价和运输选项,但没有添加两者。另一个给出NaN。也许您需要更多代码,因为回显代码属于带有更多代码的购物车插件。

4

4 回答 4

0

I have tryed playing arround with the old code i made. And could not find what was wrong with it, but i have posted a total type of script now. "Remember you have to have the jquery-1.8.2 java library included like:

<?
$totalPrice = '100';
$totalShipping = '100';
$currencySymbol = '100';
$decimalPoint = '100';
$thousandsSeperator = '100';
?>
<script type="text/javascript" src="inset the link til the jquery 1.8.2 javascript file here." ></script>
<script>
$(document).ready(function() {
    $('.calculate').change(function() {
        var total = 0;
        $('.calculate').each(function() {
            var value = $(this).val();
               if(value != 0) {
                 total += parseFloat(value);
            }
        });
        total = total + parseFloat(<?php echo $totalPrice; ?>);
        total = total + parseFloat(<?php echo $totalShipping; ?>);
        total = total + parseFloat(<?php echo $currencySymbol; ?>);
        total = total + parseFloat(<?php echo $decimalPoint; ?>);
        total = total + parseFloat(<?php echo $thousandsSeperator; ?>);
        $('#total').text('' + total.toFixed(0));
    });
});
</script>

<select class="calculate" name="shipper">
<option value="25">Netherlands</option>
<option value="49">Europe</option>
</select>
<br>
<span id="total"></span>

You can decide the data of the strings yourself in the top of the file. But this one is testet and are working on a apache server with the latest updates of php5 and apache.

于 2012-12-09T15:52:19.360 回答
0

您可能希望为此使用 jQuery - 每次“select[name=shipper]”更改时,更新您的价格。您可能有一个不可见的成本价值,但运输和只需使用 jQuery 更新它,就像这样:

<script>
    $("select[name=shipper]").change(function() {
        var value=$(this).val();
        var cost = $("input[name=cost]").val();
        document.getElementById('cost').innerHTML = cost+value;
    }
</script>

<input type="hidden" name="cost" value="<?php your_cost(); ?>" />
<select name="shipper">
    <option value="25">Netherlands</option>
    <option value="49">Europe</option>
</select>

<td><strong id="cost"></strong></td>
于 2012-12-09T13:20:16.877 回答
0

使用 JQuery 的 Ajax 方法

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
        $(document).ready(function()
        {
            $.calTotal = function()
            {
                var shipper = $('#shipper').val();
                $.get('http://localhost/ajax/calTotal.php?shipper='+shipper, function(data) {
                    $('#cost').html(data);              
                });
            }

            $("#shipper").change($.calTotal);
            $.calTotal(); //Calling calTotal Method to get Amount for default shipper
        });
    </script>

    <table>
    <select id="shipper" name="shipper">
        <option value="25">Netherlands</option>
        <option value="49">Europe</option>
    </select>   

    <td><strong>
    <span id='cost'></span>
    </strong></td>
    </table>

PHP 文件 (http://localhost/ajax/calTotal.php)

<?php 
    if(isset($_GET['shipper'])){
        $shipper_cost = $this->getShippingCost($_GET['shipper']);   
        echo $this->output_currency(
            $totalPrice + $totalShipping + $shipper_cost, 
            $currencySymbol, 
            $decimalPoint, 
            $thousandsSeperator
        );
    }else{
        echo 'Please select shipper';
    }
?>

您必须定义 getShippingCost() 方法以从数据库中获取托运人成本。

于 2012-12-09T14:13:15.107 回答
0
<table>
   <select name="shipper">
      <option value="25" data-cost="1.99">Netherlands</option>
      <option value="49" data-cost="5.99">Europe</option>
   </select>

//这里包含jquery库

<script>
   var totalPrice = '<?php echo $totalPrice; ?>';
   var totalShipping = '<?php echo $totalShipping; ?>';
   var shipper = '<?php echo $shipper; ?>';
   var currencySymbol = '<?php echo $currencySymbol; ?>';  
   var decimalPoint = '<?php echo $decimalPoint; ?>';
   var thousandsSeperator = '<?php echo $thousandsSeperator; ?>';

   $(function(){
      $('select[name=shipper]').change(function(){
         var shipping_append = $(this).children('option:selected').data('cost');
         totalShipping += shipping_append;
         var text_string = ((totalPrice*1) + (totalShipping*1))
                           + ' ' + shipper + ' ' + currencySymbol + decimalPoint + thousandsSeperator.

         $('#output').text(text_string);
      });
   });
</script>

<td id="output"><strong>
  <?php 
    echo $this->output_currency(
      $totalPrice + $totalShipping + $shipper, 
      $currencySymbol, 
      $decimalPoint, 
      $thousandsSeperator) 
  ?></strong></td>
</table>
于 2012-12-09T13:37:53.780 回答