calcul上的javascript错误

时间:2017-01-21 22:16:05

标签: javascript php

我对这个js有错误。 我在下拉列表中选择一个taxe类,并更新了taxe的更新值 我更改了我的taxe的值,并更新了taxe的值 如果我输入成本的价值,保证金的计算也不起作用。 如何解决这些元素

https://jsfiddle.net/oitsuki/pLofhns3/

谢谢

通过控制台我有这些错误

        try {
            socket.connect(addr, getUrlConnectTimeout());
        } catch (Error e) {
            if (e.getCause() instanceof InvocationTargetException) {
                InvocationTargetException tempInvocationTargetException = (InvocationTargetException)e.getCause();
                if (tempInvocationTargetException.getTargetException() instanceof IOException) {
                    // Extract the original IOException, see
                    // http://stackoverflow.com/questions/40921296/java-proxy-authentication-ioexception-not-being-thrown/
                    throw (IOException)tempInvocationTargetException.getTargetException();
                }
            }
            // rethrow the original error
            throw e;
        }

我的HTML代码

updateGross is not defined
updateMargin is not defined


  $tax_class_drop_down =  Tax::TaxClassDropDown();
?>
            <script type="text/javascript"><!--
              var tax_rates = new Array();
<?php
              for ($i=0, $n=sizeof($tax_class_drop_down); $i<$n; $i++) {
                if ($tax_class_drop_down[$i]['id'] > 0) {
                  echo 'tax_rates["' . $tax_class_drop_down[$i]['id'] . '"] = ' . Tax::displayTaxRateValue($tax_class_drop_down[$i]['id']) . ';' . "\n";
                }
              }
?>

              function doRound(x, places) {
                return Math.round(x * Math.pow(10, places)) / Math.pow(10, places);
              }

              function getTaxRate() {
                var selected_value = document.forms["new_product"].products_tax_class_id.selectedIndex;
                var parameterVal = document.forms["new_product"].products_tax_class_id[selected_value].value;

                if ( (parameterVal > 0) && (tax_rates[parameterVal] > 0) ) {
                  return tax_rates[parameterVal];
                } else {
                  return 0;
                }
              }

              function updateGross() {
                var taxRate = getTaxRate();
                var grossValue = document.forms["new_product"].products_price.value;
                if (taxRate > 0) {
                  grossValue = grossValue * ((taxRate / 100) + 1);
                }

<?php
            // Desactivation du module B2B
            if (MODE_B2B_B2C == 'true') {
              $QcustomersGroup = $OSCOM_Db->prepare('select distinct customers_group_id,
                                                                     customers_group_name,
                                                                     customers_group_discount
                                                     from :table_customers_groups
                                                     where customers_group_id != 0
                                                     order by customers_group_id
                                                    ');

              $QcustomersGroup->execute();

              while ($QcustomersGroup->fetch() ) {
?>
                var grossValue<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?> = document.forms["new_product"].price<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?>.value;

                if (taxRate > 0) {
                  grossValue<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?> = grossValue<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?> * ((taxRate / 100) + 1);
                }

                document.forms["new_product"].price_gross<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?>.value = doRound(grossValue<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?>, 4);
<?php
              }
            }
?>
                document.forms["new_product"].products_price_gross.value = doRound(grossValue, 4);
         }

/********************************/
/*        Margin report   */
/********************************/
              function updateMargin() {
                var grossValue = document.forms["new_product"].products_price.value; // valeur net du prix
                var costValue = document.forms["new_product"].products_cost.value; // cout d'achat
                var handlingValue = document.forms["new_product"].products_handling.value; // manutention ou autres frais

                if (isNaN(costValue)) costValue=0;
                if (isNaN(handlingValue)) handlingValue=0;

                marginValue =  100 - ((( parseInt(costValue) + parseInt(handlingValue)) /  parseInt(grossValue)) * 100);
                marginValue = Math.round(marginValue,2);
                document.getElementById('products_price_margins').innerHTML = marginValue + "%";
              }

              function updateNet() {
                var taxRate = getTaxRate();
                var netValue = document.forms["new_product"].products_price_gross.value;

                if (taxRate > 0) {
                  netValue = netValue / ((taxRate / 100) + 1);
                }

<?php
// Desactivation du module B2B
        if (MODE_B2B_B2C == 'true') {
          $QcustomersGroup = $OSCOM_Db->prepare('select distinct customers_group_id,
                                                                customers_group_name,
                                                                customers_group_discount
                                                 from :table_customers_groups
                                                 where customers_group_id != 0
                                                 order by customers_group_id
                                                ');

          $QcustomersGroup->execute();

          while ($QcustomersGroup->fetch() ) {
?>
                var netValue<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?> = document.forms["new_product"].price_gross<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?>.value;

                if (taxRate > 0) {
                  netValue<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?> = netValue<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?> / ((taxRate / 100) + 1);
                }

                document.forms["new_product"].price<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?>.value = doRound(netValue<?php echo $QcustomersGroup->valueInt('customers_group_id'); ?>, 4);
<?php
          }
        }
?>

              document.forms["new_product"].products_price.value = doRound(netValue, 4);
            }
//--></script>

            <div class="tab-pane" id="tab3">
              <div class="col-md-12 mainTitle">
                <span><?php echo OSCOM::getDef('text_products_price_public'); ?></span>
              </div>
              <div class="adminformTitle" style="height:100%; padding-left:0px; padding-right:0px; padding-bottom:0px; padding-top:0px;">
                <div style="background-color:#ebebff; height:100%;">
                  <div>&nbsp;</div>
                  <div>
                    <span class="col-md-2"><?php echo OSCOM::getDef('text_products_tax_class'); ?></span>
                    <span class="col-md-2"><?php echo HTML::selectMenu('products_tax_class_id', $tax_class_drop_down, $pInfo->products_tax_class_id, 'onchange="updateGross()"'); ?></span>
                  </div>
                  <div>&nbsp;</div>
                  <div class="row">
                    <div class="col-md-12">
                      <span class="col-md-2"><?php echo OSCOM::getDef('text_products_price'); ?></span>
<?php
      if (DISPLAY_DOUBLE_TAXE == 'false') {
        echo '<span class="col-md-2">'. HTML::inputField('products_price', $pInfo->products_price, 'onkeyup="updateGross()"') . '<strong>' . OSCOM::getDef('text_products_price_net') . '</strong></span>';
        echo '<span class="col-md-2">'.  HTML::inputField('products_price_gross', $pInfo->products_price, 'onkeyup="updateNet()"') . '<strong>' . OSCOM::getDef('text_products_price_gross') . '</strong></span>';
      } else {
        echo '<span class="col-md-2">' . HTML::inputField('products_price', $pInfo->products_price, 'onkeyup="updateGross()"') . '<strong>' . OSCOM::getDef('text_products_price_net') . '</strong></span>';
      }
?>

1 个答案:

答案 0 :(得分:1)

修正1:

tax_rates["3"] = 3.00%;     // ---> incorrect
tax_rates["3"] = 3.00;      // ---> correct 


修复2:
HTML不包含任何名为new_product的表单。因此,现在有两个选项 - 直接使用其id定义表单或访问元素。如果您确实选择定义表单以包含HTML元素,那么访问方法将与您到目前为止所遵循的方法相同:

document.forms["new_product"].products_handling.value;


修正3:
如果为HTML元素定义name,则可以使用document.getElementByName访问它。同样,如果您定义id,则可以使用document.getElementById访问它。最好定义nameid两者,以便您(甚至脚本)不会被混淆。这就是为什么会出现not defined错误的原因。

因此,更新了JS:

    var tax_rates = new Array();
    tax_rates["3"] = 3.00;
    tax_rates["4"] = 4.00;
    tax_rates["1"] = 1.00;
    tax_rates["2"] = 2.00;

    function doRound(x, places) {
        return Math.round(x * Math.pow(10, places)) / Math.pow(10, places);
    }

    function getTaxRate() {
        //var selected_value = document.forms["new_product"].products_tax_class_id.selectedIndex;
        parameterVal = document.getElementById("products_tax_class_id").value;
        if ( (parameterVal > 0) && (tax_rates[parameterVal] > 0) ) {
            return tax_rates[parameterVal];
        }
        else {
            return 0;
        }
    }

    function updateGross() {
        var taxRate = getTaxRate();
        var grossValue = document.getElementById("products_price").value;
        if (taxRate > 0) {
            grossValue = grossValue * ((taxRate / 100) + 1);
        }
        document.getElementById("products_price_gross").value = doRound(grossValue, 4);
    }

/********************************/
/*        Margin report         */
/********************************/
    function updateMargin() {
        var grossValue = document.getElementById("products_price").value; // valeur net du prix
        var costValue = document.getElementById("products_cost").value; // cout d'achat
        var handlingValue = document.getElementById("products_handling").value; // manutention ou autres frais

        if (isNaN(costValue)) costValue=0;
        if (isNaN(handlingValue)) handlingValue=0;

        marginValue =  100 - ((( parseInt(costValue) + parseInt(handlingValue)) /  parseInt(grossValue)) * 100);
        marginValue = Math.round(marginValue,2);
        document.getElementById("products_price_margins").innerHTML = marginValue + "%";
    }

    function updateNet() {
        var taxRate = getTaxRate();
        var netValue = document.getElementById("products_price_gross").value;

        if (taxRate > 0) {
            netValue = netValue / ((taxRate / 100) + 1);
        }

        document.getElementById("products_price").value = doRound(netValue, 4);
    }


请注意,我在上面的脚本中没有考虑过price1price_gross1等元素,因为它们没有相应的HTML元素。

工作示例:
http://codepen.io/anon/pen/pRwMyG