2011-12-21 9 views
1

を計算する:jQueryのは、私はこのようなテーブルに何かを持っている1つの関数に

sellingprice cost postage profit paypal ebayfees discount FVF 
    ?   49  1.75  4.00  ?   ?   0.2  ?  
    ?   51  1.75  4.00  ?   ?   0.2  ?  

これは、上記の情報を解決するための異なる式です:

sellingpriceresult =コスト+送料+利益+ペイパル+ FVF;
paypalresult = sellpriceresult * 0.022 + 0.3;
FVFresult = ebayfees - (ebayfees * discount);条件は、それは結果だ。このような何かを表示する場合
ebayfeesresultを使用して次のようになります。

をもしsellingpriceresult> 50それから(sellingpriceresult - 50)*他に0.05 + 3.5

場合sellingpriceresult < = 50、その後sellingpriceresult * 0.07

クライアント側計算を使用して計算を実装したいと考えています。私の問題は、私はどのように1つの関数ですべての計算を組み合わせるのか分からないということです。私はjQueryについて少し考えており、皆さんにこの方法を教えてもらいたいと思います。どんな助けでももっと感謝します。ここで

は私のサンプルコードは次のとおりです。

<table border = "0">  
      <tr> 
       <td><center>Selling Price</center></td> 
       <td><center>Cost</center></td> 
       <td><center>Postage</center></td> 
       <td><center>Profit</center></td> 
       <td><center>Paypal</center></td> 
       <td><center>eBay Fees</center></td> 
       <td><center>Discount</center></td> 
       <td><center>FVF</center></td> 
      </tr> 
       <tr> 
        <td> 
         <input type='text' id='sellingprice' name='sellingprice' size='10' readonly='true'/> 
        </td> 
        <td> 
         <input type='text' id='cost' name='cost' size='10' value='$myrow[1]' readonly='true'/> 
        </td> 
        <td> 
         <input type='text' id='postage' name='postage' size='10' value='1.75' readonly='true'/> 
        </td> 
        <td> 
         <input type='text' id='profit' name='profit' size='10' value='4.00' readonly='true'/> 
        </td> 
        <td> 
         <input type='text' id='paypal' name='paypal' size='10' readonly='true'/> 
        </td> 
        <td> 
         <input type='text' id='ebayfees' name='ebayfees' size='10' readonly='true'/> 
        </td> 
        <td> 
         <input type='text' id='discount' name='discount' size='10' value='0.2' readonly='true'/> 
        </td> 
        <td> 
         <input type='text' id='fvf' name='fvf' size='10' readonly='true'/> 
        </td> 
       </tr> 
     </table> 

    <script> 

     $(document).ready(function(){ 
      $('tr').each(function(){ 
      var sellingprice = 0; 
      $(this).find("input[name=cost],input[name=postage],input[name=profit],input[name=paypal],input[name=fvf]").each(function(){ 
       sellingprice += (+$(this).val()); 
      }); 
      $(this).find("input[name=sellingprice]").val(sellingprice).css("background-color", "yellow"); 
      });  
     }); 

     $(document).ready(function(){ 
      $('tr').each(function(){ 
      var paypal = 0; 
      $(this).find("input[name=sellingprice]").each(function(){ 
       paypal = (+$(this).val()) * 0.022 + 0.3; 
       paypal = paypal.toFixed(2); 
      }); 
      $(this).find("input[name=paypal]").val(paypal).css("background-color", "yellow"); 
      }); 
     }); 


     $(document).ready(function(){ 
      $('tr').each(function(){ 
      var sellingprice = 0; 
      $(this).find("input[name=cost],input[name=postage],input[name=profit],input[name=paypal],input[name=fvf]").each(function(){ 
       sellingprice = (+$(this).val()); 
       sellingprice = sellingprice.toFixed(2); 
      }); 
      $(this).find("input[name=sellingprice]").val(sellingprice).css("background-color", "yellow"); 
      });  
     }); 

     $(document).ready(function(){ 
      $('tr').each(function(){ 
      var fvf = 0; 
      $(this).find("input[name=ebayfees],input[name=discount]").each(function(){ 
       fvf = (+$(this).val()); 
      }); 
      $(this).find("input[name=fvf]").val(fvf).css("background-color", "yellow"); 
      }); 
     }); 


     $(document).ready(function(){ 
      $('tr').each(function(){ 
      var sellingprice = 0; 
      $(this).find("input[name=cost],input[name=postage],input[name=profit],input[name=paypal],input[name=fvf]").each(function(){ 
       sellingprice += (+$(this).val()); 
      }); 
      $(this).find("input[name=sellingprice]").val(sellingprice).css("background-color", "yellow"); 
      }); 
     }); 

     $(document).ready(function(){ 
      $('tr').each(function() { 
       var sellingpriceResult = parseFloat($(this).find("input[name=sellingprice]").val()); 

       var result = 0; 
       if (sellingpriceResult > 50) { 
        result = (sellingpriceResult - 50) * 0.05 + 3.5; 
        result = result.toFixed(2); 
       } 
       else { 
        result = sellingpriceResult * 0.07; 
        result = result.toFixed(2); 
       } 
       $(this).find("input[name=ebayfees]").val(result).css("background-color", "yellow"); 
      }); 
     }); 
    </script> 
+0

コストの値は '$ myrow [1]'です...私はそれがtypohだと思います。 –

+0

コストコラムを記入するためにデータベースにフェッチされているので – jovazel

答えて

0

注1:私はあなたの入力からID属性を削除する必要がありますので、IDは内で一意である必要がありますので、いくつかの行があるように起こっている推測していますHTMLドキュメント。

注2:私はあなたがしたい計算を完全に理解していません。 「売り価格」は「FVF」に依存し、「FVF」は「Ebay fee」と「Ebay fee」に依存します。「Selling price」に依存します...この再帰的依存性で計算するのは少し難しいですね

注3:あなたの例ではinsertionfee要素を使用していますが、私が指定したマークアップにはありません。

次のコードに関する注意:

  • $this = $(this)

  • で再使用するために現在の<tr>のためのjQueryオブジェクトを取得するだけでなく、再利用のために、すべての必要な要素を見つけます。それは、より効率的であるほか、この$item.val(myvariable = 1+1)をやっ$this

  • のおかげで、実際にmyvariableに計算を節約し、.val()

  • に渡し、このcondition ? <if condition true> : <if condition false>は、あなたは私ができなかったインラインconditionnal文

  • を行うことができます.toFixed(2)を使用するには、あなたの関数であると思います。

私はjsfiddleをあなたに見せてくれました。私は、Excelファイルをチェックして

式について



$(document).ready(function() { 

    $('tr').each(function() { 

     var $this = $(this), 
      $sellingprice = $this.find('[name=sellingprice]'), 
      $cost = $this.find('[name=cost]'), 
      $postage = $this.find('[name=postage]'), 
      $profit = $this.find('[name=profit]'), 
      $paypal = $this.find('[name=paypal]'), 
      $ebayfees = $this.find('[name=ebayfees]'), 
      $discount = $this.find('[name=discount]'), 
      $fvf = $this.find('[name=fvf]'); 

     var selpriceresult, paypalresult, fvfresult, ebayresult; 

     $sellingprice.val((selpriceresult = $cost.val() + $postage.val() + $paypal.val() + $fvf.val())); 
     $paypal.val(paypalresult = selpriceresult * 0.022 + 0.3); 

     $ebayfees.val(ebayresult = selpriceresult > 50 
         ? (selpriceresult - 50) * 0.05 + 3.5 
         : selpriceresult * 0.07); 

     $fvf.val(fvfresult = ebayresult - (ebayresult - $discount.val())); 

}); 
。これはそう簡単ではありません。数式には依存関係があり、どのようにExcelが実際に内部的に計算を実行しているかはわかりません。

私が知っているのは、依存関係ツリーをビルドして最適化して、ワークブックの計算方法を決定することです。あなたは、依存関係とセル間の関係を並べ替えることができますが(Tools> Audit)、私はExcelの達人ではなく、私の知るところではありません。

私はこれもjqueryコードブロックを1つに結合することであったこの質問の範囲外であると考えます。 javascript計算にExcelの式を変換してください。

私は本当にあなたも計算にあなたを助けて喜んでいますが、私はできません。この特定の主題について別の質問をすることもできます。

+0

注3:私のマークアップでは挿入文字を残念に思ってしまいました。これはマークアップには含まれていません。 – jovazel

+0

それは本当です。 Sellingpriceはpaypalとfvfの出力に依存します。 ebayfeesとpaypalはどちらもsellerpriceの出力に依存します。あなたがこの問題の正しい計算を得るまで私を導くことができますか? – jovazel

+0

私は式を決定するためにあなたをどのように導くことができるのか分かりませんが、あなたは彼らが何であるかを知っています。私はコードで書く方法についてだけあなたを導くことができます。 –

関連する問題