2015-06-22 87 views
1

我想幫助一個朋友修復他的網站,他們要求我幫忙整理一個計算平方米的表格計算器...現在它完全計算它。我的伴侶希望它能夠平方米。我不知道代碼,有沒有人可以幫我找到一種方法來收集平方米?平方米計算器幫助捨去平方米

這是我們迄今爲止;

<script type="text/javascript">// <![CDATA[ 
 

 
    (function($){ 
 
    jQuery(document).ready(function($){ 
 

 
    
 
    theUnit = 'cms'; 
 
     $('#inserted-height').val(''); 
 
     $('#inserted-width').val(''); 
 

 
     $('#inserted-height').focus(function(){ 
 
      $(this).val(''); 
 
    }); 
 
     $('#inserted-width').focus(function(){ 
 
     $(this).val(''); 
 
    }); 
 

 
     $('#inserted-height').blur(function(){ 
 
     var height = $(this).val(); 
 
     if(!checkDigits(height)){ 
 
      $(this).focus(); 
 
      $('#calculate-button').removeClass("Reset"); 
 
      return false; 
 
     } 
 
    }); 
 

 
     $('#inserted-width').blur(function(){ 
 
     var width = $(this).val(); 
 
     if(!checkDigits(width)){ 
 
      $(this).focus(); 
 
      $('#calculate-button').removeClass("Reset"); 
 
      return false; 
 
     } 
 
    }); 
 

 
    }); 
 

 
     $('#calculate-button').click(function(){ 
 

 
      if ($(this).hasClass("Reset")){ 
 

 
       $('#screen-two').fadeOut(function(){ 
 
        $('#cms-message').fadeIn(); 
 
       }); 
 

 
       $(this).removeClass("Reset"); 
 
       $(this).val("Submit"); 
 
       $('#inserted-height').val(''); 
 
       $('#inserted-width').val(''); 
 
      return false; 
 
     } 
 
     /* SET SIZES */ 
 
     var MIN_SIZE = 20000; 
 
     var MAX_SIZE = 3000000; 
 

 
     /* FREE SHIPPING */ 
 
     
 

 
     var currentProductPrice = $('#currentProductPricefromCMS').val(); 
 
     var estimatedWidth = $('#inserted-width').val(); 
 
     var estimatedHeight = $('#inserted-height').val(); 
 
     /* added pre price to calculator */ 
 
     var currencySymbol = "AUD$"; 
 
     var totalSize = estimatedWidth * estimatedHeight; 
 
     var isInches = false; 
 
     // get the unit type 
 
     var theUnits = $("input[name='unit']:checked").val() 
 
     // console.log(theUnits); 
 

 
     // boolean for valid inputs 
 
     var valid = false; 
 

 
     if(theUnit == 'inch') { 
 
      isInches = true; 
 
      totalSize = (estimatedWidth* 2.54) * (estimatedHeight * 2.54); 
 
     } else { 
 
      isInches = false; 
 
     } 
 
     /* WARNING FOR MAX AND MIN SIZES */ 
 

 
     if((totalSize < MIN_SIZE || totalSize > MAX_SIZE) && !isInches){ 
 
      alert('Oops... size must be within '+MIN_SIZE/10000+' sqm and '+MAX_SIZE/10000+' sqm. \n Note: 100 cms = 1 Metre.'); 
 
      $('#inserted-width').css({'color':' #AA202E'}); 
 
      $('#inserted-height').css({'color':' #AA202E'}); 
 
      return false; 
 
     } else { 
 

 
      if((totalSize < MIN_SIZE || totalSize > MAX_SIZE) && isInches){ 
 
       alert('Oops... size must be within '+MIN_SIZE/10000+' sqm and '+MAX_SIZE/10000+' sqm or approx 27 square feet (MIN) and 323 square feet (MAX). Note: 12 inches = 1 Foot.'); 
 
       $('#inserted-width').css({'color':'#AA202E'}); 
 
       $('#inserted-height').css({'color':'#AA202E'}); 
 
       return false; 
 
      } else { 
 
       valid = true; 
 
      } 
 
     } 
 

 
     /* DO IT */ 
 
     // can only proceed if true 
 
     if(valid){ 
 
      //fade in price 
 

 
      $('#cms-message').fadeOut(function(){ 
 
       $('#screen-two').fadeIn(); 
 
      }); 
 
      $('#calculate-button').val("Reset"); 
 
      $('#calculate-button').addClass("Reset"); 
 

 
      /* IF INCHES */ 
 

 
      if(isInches) { 
 
       // console.log("inches selected"); 
 
       var InchesToCms = (totalSize); 
 
       var estimatedPrice = InchesToCms * currentProductPrice; 
 
       // console.log("inches to cms"+totalSize); 
 
       var outputPrice = estimatedPrice/10000; 
 
       var actualPrice = outputPrice.toFixed(2); 
 
       $('#display-price').val(currencySymbol+actualPrice); 
 
      } 
 
      /* IF CM */ 
 
      else { 
 
       // console.log("cms selected"); 
 
       var estimatedPrice = totalSize * currentProductPrice; 
 
       /* added for rounding up */ 
 
\t \t \t \t var totalSize = Math.round(outputPrice); 
 
       var outputPrice = estimatedPrice/10000; 
 
       
 
       var actualPrice = outputPrice.toFixed(2); 
 

 

 
       $('.symbol').html("<span>" + currencySymbol + "</span >"); 
 
       $('#display-price').val(actualPrice); 
 
      } 
 
      var free_shipping_min = 330; 
 
      /* check if above $400 for actual price */ 
 
      if (actualPrice >= free_shipping_min){ 
 
       $(".calculator_disclaimer").html('<b>Price includes <span>FREE</span> shipping</b>'); 
 

 
      } else { 
 

 
       $(".calculator_disclaimer").html("Price excludes shipping $49.95"); 
 
      } 
 

 
     } 
 

 
    }); 
 

 
    //REGEX FOR Checking if letters entered. 
 
    function checkDigits(str){ 
 

 
     var intRegex = /[A-z]/; 
 
     if(intRegex.test(str)) { 
 
      alert('Please enter only numbers'); 
 
      return false; 
 
     } else { 
 
      return true; 
 
     } 
 

 
    } 
 

 

 
    /* RESET BUTTON */ 
 
     $('.reset').click(function($){ 
 
     totalSize = 0; 
 
     $('#inserted-width').css({'color':'solid 1px #969696'}); 
 
     $('#inserted-height').css({'border':'solid 1px #969696'}); 
 
     
 
     $('#inserted-height').val(''); 
 
     $('#inserted-width').val(''); 
 
    }); 
 

 
    /* TOGGLE BETWEEN INCHES AND CMS */ 
 

 
     $('#radio-inches').click(function($){ 
 
     $('.units').val('Inches'); 
 
     theUnit = 'inch'; 
 
    }); 
 

 
     $('#radio-cms').click(function(){ 
 
     $('.units').val('Cms'); 
 
     theUnit = 'cms'; 
 
    }); 
 

 
    })(jQuery); 
 
// ]]></script>

好吧,這是因爲什麼建議,我們有一次我所做的更改。

<div class="quick-quote"> 
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
 
<div class="home_price_calculator" style="text-align: center;"> 
 
<h3 style="text-align: center;">Quick Quote</h3> 
 
Please enter your wall size in centimetres. <!-- HIDDEN FIELDS GETS THE CURRENT PRICE PER SQUARE METRE --> <input id="currentProductPricefromCMS" type="hidden" autocomplete="off" value="55.000" /> <input id="inserted-width" class="validate" type="text" autocomplete="off" placeholder="length" size="9" value="" /> X <input id="inserted-height" class="validate" type="text" autocomplete="off" placeholder="height" size="9" value="" /> <input id="calculate-button" class="calculate" type="button" value="Submit" name="calculate" /> <!-- <div id="cms-message">Please enter your size in centimetres.</div> --> <input id="display-currency" type="hidden" value="" />$AUD <input id="display-price" type="text" readonly="readonly" size="9" /> 
 
<div class="calculator_disclaimer" style="text-align: center;">&nbsp;</div> 
 
</div> 
 
</div> 
 
<script type="text/javascript">// <![CDATA[ 
 
/* SETS CENTIMETRES ONLOAD */ 
 
    (function($){ 
 
    jQuery(document).ready(function($){ 
 

 
    /* EXECUTION OF PRICING ESTIMATOR */ 
 
    theUnit = 'cms'; 
 
     $('#inserted-height').val(''); 
 
     $('#inserted-width').val(''); 
 

 
     $('#inserted-height').focus(function(){ 
 
      $(this).val(''); 
 
    }); 
 
     $('#inserted-width').focus(function(){ 
 
     $(this).val(''); 
 
    }); 
 

 
     $('#inserted-height').blur(function(){ 
 
     var height = $(this).val(); 
 
     if(!checkDigits(height)){ 
 
      $(this).focus(); 
 
      $('#calculate-button').removeClass("Reset"); 
 
      return false; 
 
     } 
 
    }); 
 

 
     $('#inserted-width').blur(function(){ 
 
     var width = $(this).val(); 
 
     if(!checkDigits(width)){ 
 
      $(this).focus(); 
 
      $('#calculate-button').removeClass("Reset"); 
 
      return false; 
 
     } 
 
    }); 
 

 
    }); 
 

 
     $('#calculate-button').click(function(){ 
 

 
      if ($(this).hasClass("Reset")){ 
 

 
       $('#screen-two').fadeOut(function(){ 
 
        $('#cms-message').fadeIn(); 
 
       }); 
 

 
       $(this).removeClass("Reset"); 
 
       $(this).val("Submit"); 
 
       $('#inserted-height').val(''); 
 
       $('#inserted-width').val(''); 
 
      return false; 
 
     } 
 
     /* GETS INITIAL SIZES */ 
 
     var MIN_SIZE = 20000; 
 
     var MAX_SIZE = 3000000; 
 

 
     /* FREE MIN SHIPPING VARIABLE */ 
 
     
 

 
     var currentProductPrice = $('#currentProductPricefromCMS').val(); 
 
     var estimatedWidth = $('#inserted-width').val(); 
 
     var estimatedHeight = $('#inserted-height').val(); 
 
     /* added pre price to calculator */ 
 
     var currencySymbol = "AUD$"; 
 
     /* next line as per advice */ 
 
     var totalSize = Math.ceil((estimatedWidth * estimatedHeight)); 
 
     var isInches = false; 
 
     // get the unit type 
 
     var theUnits = $("input[name='unit']:checked").val() 
 
     // console.log(theUnits); 
 

 
     // boolean for valid inputs 
 
     var valid = false; 
 

 
     if(theUnit == 'inch') { 
 
      isInches = true; 
 
       /* next line as per advice */ 
 
      totalSize = Math.ceil((estimatedWidth * 0.0254) * (estimatedHeight * 0.0254)); 
 
     } else { 
 
      isInches = false; 
 
     } 
 
     /* WARNING FOR MAX AND MIN SIZES */ 
 

 
     if((totalSize < MIN_SIZE || totalSize > MAX_SIZE) && !isInches){ 
 
      alert('Oops... size must be within '+MIN_SIZE/10000+' sqm and '+MAX_SIZE/10000+' sqm. \n Note: 100 cms = 1 Metre.'); 
 
      $('#inserted-width').css({'color':' #AA202E'}); 
 
      $('#inserted-height').css({'color':' #AA202E'}); 
 
      return false; 
 
     } else { 
 

 
      if((totalSize < MIN_SIZE || totalSize > MAX_SIZE) && isInches){ 
 
       alert('Oops... size must be within '+MIN_SIZE/10000+' sqm and '+MAX_SIZE/10000+' sqm or approx 27 square feet (MIN) and 323 square feet (MAX). Note: 12 inches = 1 Foot.'); 
 
       $('#inserted-width').css({'color':'#AA202E'}); 
 
       $('#inserted-height').css({'color':'#AA202E'}); 
 
       return false; 
 
      } else { 
 
       valid = true; 
 
      } 
 
     } 
 

 
     /* DO IT */ 
 
     // can only proceed if true 
 
     if(valid){ 
 
      //fade in price 
 

 
      $('#cms-message').fadeOut(function(){ 
 
       $('#screen-two').fadeIn(); 
 
      }); 
 
      $('#calculate-button').val("Reset"); 
 
      $('#calculate-button').addClass("Reset"); 
 

 
      /* IF INCHES SELECTED */ 
 

 
      if(isInches) { 
 
       // console.log("inches selected"); 
 
       var InchesToCms = (totalSize); 
 
       var estimatedPrice = InchesToCms * currentProductPrice; 
 
       // console.log("inches to cms"+totalSize); 
 
       var outputPrice = estimatedPrice/10000; 
 
       var actualPrice = outputPrice.toFixed(2); 
 
       $('#display-price').val(currencySymbol+actualPrice); 
 
      } 
 
      /* IF CM SELECTED */ 
 
      else { 
 
       // console.log("cms selected"); 
 
       var estimatedPrice = Math.round((totalSize) * (currentProductPrice)); 
 
       var outputPrice = estimatedPrice/10000; 
 
       
 
       var actualPrice = outputPrice.toFixed(2); 
 
/* added for rounding up */ 
 
\t \t \t \t var outputPrice = Math.round(outputPrice); 
 

 
       $('.symbol').html("<span>" + currencySymbol + "</span >"); 
 
       $('#display-price').val(actualPrice); 
 
      } 
 
      var free_shipping_min = 330; 
 
      /* check if above $400 for actual price */ 
 
      if (actualPrice >= free_shipping_min){ 
 
       $(".calculator_disclaimer").html('<b>Price includes <span>FREE</span> shipping</b>'); 
 

 
      } else { 
 

 
       $(".calculator_disclaimer").html("Price excludes shipping $49.95"); 
 
      } 
 

 
     } 
 

 
    }); 
 

 
    //REGEX FOR Checking if letters entered. 
 
    function checkDigits(str){ 
 

 
     var intRegex = /[A-z]/; 
 
     if(intRegex.test(str)) { 
 
      alert('Please enter only numbers'); 
 
      return false; 
 
     } else { 
 
      return true; 
 
     } 
 

 
    } 
 

 

 
    /* RESET BUTTON */ 
 
     $('.reset').click(function($){ 
 
     totalSize = 0; 
 
     $('#inserted-width').css({'color':'solid 1px #969696'}); 
 
     $('#inserted-height').css({'border':'solid 1px #969696'}); 
 
     /* added by coops 1 Feb 2013 */ 
 
     $('#inserted-height').val(''); 
 
     $('#inserted-width').val(''); 
 
    }); 
 

 
    /* TOGGLE BETWEEN INCHES AND CMS */ 
 

 
     $('#radio-inches').click(function($){ 
 
     $('.units').val('Inches'); 
 
     theUnit = 'inch'; 
 
    }); 
 

 
     $('#radio-cms').click(function(){ 
 
     $('.units').val('Cms'); 
 
     theUnit = 'cms'; 
 
    }); 
 

 
    })(jQuery); 
 
// ]]></script> 
 
<div class="bottom_tab">&nbsp;</div> 
 
<div class="bottom_tab" style="text-align: center;">&nbsp;</div>

+1

「我固定了一個網站的朋友」 和「我不知道t代碼「在同一段落中。這怎麼可能? – Tomalak

+1

嘗試谷歌搜索_round javascript_ - 它返回作爲命中#1的答案。另外,如果平方米的數量是1.01呢?你真的想要返回2嗎? – enhzflep

+0

輪*上升*或只是輪?例如2.1是2還是3? –

回答

2

我建議採取先在JS教程來看,因爲你缺乏經驗。雖然我很佩服信心!

請注意,您可以使用單詞ceil或單詞round:ceil將1.01,1.5等輪到2,並且輪將1.01,1.49等輪到1和1.5,1.99等等直到2。


更改的行:

var totalSize = estimatedWidth * estimatedHeight; 

到:

var totalSize = Math.round((estimatedWidth/100) * (estimatedHeight/100)); 

或:

var totalSize = Math.ceil((estimatedWidth/100) * (estimatedHeight/100)); 

而行:

totalSize = (estimatedWidth* 2.54) * (estimatedHeight * 2.54); 

到:

totalSize = Math.round((estimatedWidth * 0.0254) * (estimatedHeight * 0.0254)); 

或:

totalSize = Math.ceil((estimatedWidth * 0.0254) * (estimatedHeight * 0.0254)); 
+0

感謝盧克 - 我確實嘗試過並得到了有關英寸的錯誤......因爲他的網站大多數用戶來自澳大利亞,因此使用釐米。對於CM,我只需簡單地添加這些行就可以了? JS教程當然是在卡片上 - 感謝您的輸入。 – nakerjack

+0

你必須提供更多的細節;什麼是錯誤?你想要替換這些行,而不是添加。 – Luke

+0

對不起我的錯誤,我沒有按照你所說的代替代碼,而是無意中在這個過程中犯了一個錯誤。這一次,我沒有通過替換這些行來得到任何錯誤。我確實需要將釐米部分也包括在內......我試圖替換該代碼部分中的代碼,但沒有運氣。謝謝你在這裏的時間。 – nakerjack