2015-09-23 52 views
0

Iam在Javascript中進行一些計算時卡住了。我顯示了一些記錄行,每行都有計算。實際情況是系統必須計算數量*單價並填寫總字段中的總數。當我從下拉菜單中更改貨幣時,必須經過一些條件,如果我已在javascript中給出條件,則不會執行此操作。不知道什麼是實際問題。誰能幫忙?蔭把下面的HTML表單:帶有某些條件的JQuery/Javascript計算

<script type='text/javascript' src='http://code.jquery.com/jquery-2.1.3.js'></script> 

<script type="text/javascript"> 

function isNum(value) 
{ 
return 123; 
} 

function calcTotals() 
{ 
    var total = 0; 
var i = 0; 

while (document.forms['cart'].elements['unitprice[]'][i]) 
{ 
    unitpriceObj = document.forms['cart'].elements['unitprice[]'][i]; 
    item_quantityObj = document.forms['cart'].elements['item_quantity[]'][i]; 
    total_inr_valueObj = document.forms['cart'].elements['total_inr[]'][i]; 

    totalObj = document.forms['cart'].elements['total[]'][i]; 


    totalObj.value = parseFloat((item_quantityObj.value*1) * unitpriceObj.value*1); 

     //Currency_change formulae 
     var e = document.getElementById("currency_change[]"); 
     var currency_selected = e.options[e.selectedIndex].value; 
     var price = $(this).find(':selected').data('price'); 

     if (currency_selected.value() == 'INR'){ 
      total_inr_valueObj.value=totalObj.value; 
       } else if (currency_selected.value() == 'USD'){ 
      total_inr_valueObj.value = totalObj.value * price.value; 
      } else { 
      total_inr_valueObj.value = (inrvalue.value/price.value) * totalObj.value; 
      } 
    } 

    i++; 
} 
return; 
} 

</script> 


</head> 
<body> 

<form name='cart' method='post' class='single' action='generate_quot_cust_edititems_save_complete.php?tender_id=1' > 

    <table width="100%" border="1" style="border-collapse: collapse;" cellpadding="1" cellspacing="1"> 
    <tr bgcolor="#E6E6FA"> 
    <td width=4%>SlNo</td> 
    <td width=10%>Item Name</td> 
<td width=4%>Qty</td> 
<td width=3%>Units</td> 
<td width=4%>Unitprice</td> 
<td width=6%>Currency</td> 
<td width=6%>Total</td> 
<td width=6%>Total INR</td> 
</tr> 

<tr> 
<td width='4%'> 
<input size='1' type='hidden' name='id[0]' value='' readonly/> 
<input size='1' type='text' name='sl[0]' value='1' readonly/> 
</td> 
<td width='10%'><input type='text' size='28' id='item_name0' name='item_name[0]' placeholder='filter 3' value='filter 3' /</td> 
<td width='4%'><input size='1' class='item_quantity' type='text' name='item_quantity[]' id='item_quantity[]' value='25' /></td> 
<td width='3%'><input size='1' class='item_units' type='text' name='item_units[]' id='item_units[]' value='Nos' readonly/></td> 
<td width='4%'><input size='5' class='unitprice' type='text' name='unitprice[]' id='unitprice[]' value='' onchange='calcTotals()'/></td> 
<td width='6%'> 
<select id='currency_change[]' name='currency_change[]'> 
<option value=''>select</option> 
<option value=USD data-price=1>USD</option> 
<option value=INR data-price=65.831111>INR</option> 
<option value=GBP data-price=0.643864>GBP</option> 
<option value=EUR data-price=0.88469>EUR</option> 
<option value=SGD data-price=1.398912>SGD</option> 
</select></td> 
<td width='8%'><input size='9' type='text' name='total[]' id='total[]' value='' readonly class='total'/></td> 
<td width='8%'><input size='7' type='text' id='total_inr[]' name='total_inr[]' value=''/></td> 
</tr> 
    <tr> 
    <td width='4%'><input size='1' type='hidden' name='id[1]' value='' readonly/><input size='1' type='text' name='sl[1]' value='2' readonly/></td> 
<td width='10%'><input type='text' size='28' id='item_name1' name='item_name[1]' placeholder='Filter2' value='Filter2' /</td> 
<td width='4%'><input size='1' class='item_quantity' type='text' name='item_quantity[]' id='item_quantity[]' value='15' /></td> 
<td width='3%'><input size='1' class='item_units' type='text' name='item_units[]' id='item_units[]' value='Nos' readonly/></td> 
<td width='4%'><input size='5' class='unitprice' type='text' name='unitprice[]' id='unitprice[]' value='' onchange='calcTotals()'/></td> 
<td width='6%'> 
<select id='currency_change[]' name='currency_change[]'> 
<option value=''>select</option> 
<option value=USD data-price=1>USD</option> 
<option value=INR data-price=65.831111>INR</option> 
<option value=GBP data-price=0.643864>GBP</option> 
<option value=EUR data-price=0.88469>EUR</option> 
<option value=SGD data-price=1.398912>SGD</option> 
</select> 
</td> 

<td width='8%'><input size='9' type='text' name='total[]' id='total[]' value='' readonly class='total'/></td> 
<td width='8%'><input size='7' type='text' id='total_inr[]' name='total_inr[]' value=''/></td> 
</tr> 

</table></div>  
<br><br>INR Value -><input type="text" class="inrvalue" id="inrvalue" name="inrvalue" value="65.831111"> 
<br><br> 

<table><td><input type='submit' value='--Save Data--' /></td></tr></table></form> 
+0

是什麼'add_percentageObj' –

+0

演示:http://jsfiddle.net/arunpjohny/xdrLm5ao/ –

+0

我已刪除add_percentageObj。它是用於其他計算。我已更新腳本 –

回答

0

有在腳本中多個問題。

更jQuery的上下的解決方案將是這樣的

$('#table :input').change(function() { 
 
    var $tr = $(this).closest('tr'), 
 
    $totInr = $tr.find('[name="total_inr[]"]'), 
 
    unitprice = +$tr.find('[name="unitprice[]"]').val() || 0, 
 
    qty = +$tr.find('[name="item_quantity[]"]').val() || 0, 
 
    $currency = $tr.find('[name="currency_change[]"] option:selected'), 
 
    currency = $currency.val(), 
 
    inr = $('#inrvalue').val(); 
 

 
    var total = unitprice * qty; 
 
    $tr.find('[name="total[]"]').val(total); 
 

 
    if (currency == 'INR') { 
 
    $totInr.val(total); 
 
    } else if (currency == 'USD') { 
 
    $totInr.val(total * unitprice); 
 
    } else { 
 
    $totInr.val((inr/($currency.data('price') || 1)) * total); 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<form name='cart' method='post' class='single' action='generate_quot_cust_edititems_save_complete.php?tender_id=1'> 
 
    <table width="100%" border="1" style="border-collapse: collapse;" cellpadding="1" cellspacing="1" id="table"> 
 
    <tr bgcolor="#E6E6FA"> 
 
     <td width=4%>SlNo</td> 
 
     <td width=10%>Item Name</td> 
 
     <td width=4%>Qty</td> 
 
     <td width=3%>Units</td> 
 
     <td width=4%>Unitprice</td> 
 
     <td width=6%>Currency</td> 
 
     <td width=6%>Total</td> 
 
     <td width=6%>Total INR</td> 
 
    </tr> 
 
    <tr> 
 
     <td width='4%'> 
 
     <input size='1' type='hidden' name='id[0]' value='' readonly/> 
 
     <input size='1' type='text' name='sl[0]' value='1' readonly/> 
 
     </td> 
 
     <td width='10%'> 
 
     <input type='text' size='28' id='item_name0' name='item_name[0]' placeholder='filter 3' value='filter 3' /</td> 
 
     <td width='4%'> 
 
      <input size='1' class='item_quantity' type='text' name='item_quantity[]' value='25' /> 
 
     </td> 
 
     <td width='3%'> 
 
      <input size='1' class='item_units' type='text' name='item_units[]' id='item_units[]' value='Nos' readonly/> 
 
     </td> 
 
     <td width='4%'> 
 
      <input size='5' class='unitprice' type='text' name='unitprice[]' id='unitprice[]' value='' /> 
 
     </td> 
 
     <td width='6%'> 
 
      <select id='currency_change[]' name='currency_change[]'> 
 
      <option value=''>select</option> 
 
      <option value=USD data-price=1>USD</option> 
 
      <option value=INR data-price=65.831111>INR</option> 
 
      <option value=GBP data-price=0.643864>GBP</option> 
 
      <option value=EUR data-price=0.88469>EUR</option> 
 
      <option value=SGD data-price=1.398912>SGD</option> 
 
      </select> 
 
     </td> 
 
     <td width='8%'> 
 
      <input size='9' type='text' name='total[]' id='total[]' value='' readonly class='total' /> 
 
     </td> 
 
     <td width='8%'> 
 
      <input size='7' type='text' id='total_inr[]' name='total_inr[]' value='' /> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td width='4%'> 
 
     <input size='1' type='hidden' name='id[1]' value='' readonly/> 
 
     <input size='1' type='text' name='sl[1]' value='2' readonly/> 
 
     </td> 
 
     <td width='10%'> 
 
     <input type='text' size='28' id='item_name1' name='item_name[1]' placeholder='Filter2' value='Filter2' /</td> 
 
     <td width='4%'> 
 
      <input size='1' class='item_quantity' type='text' name='item_quantity[]' id='item_quantity[]' value='15' /> 
 
     </td> 
 
     <td width='3%'> 
 
      <input size='1' class='item_units' type='text' name='item_units[]' id='item_units[]' value='Nos' readonly/> 
 
     </td> 
 
     <td width='4%'> 
 
      <input size='5' class='unitprice' type='text' name='unitprice[]' id='unitprice[]' value='' /> 
 
     </td> 
 
     <td width='6%'> 
 
      <select id='currency_change[]' name='currency_change[]'> 
 
      <option value=''>select</option> 
 
      <option value=USD data-price=1>USD</option> 
 
      <option value=INR data-price=65.831111>INR</option> 
 
      <option value=GBP data-price=0.643864>GBP</option> 
 
      <option value=EUR data-price=0.88469>EUR</option> 
 
      <option value=SGD data-price=1.398912>SGD</option> 
 
      </select> 
 
     </td> 
 
     <td width='8%'> 
 
      <input size='9' type='text' name='total[]' id='total[]' value='' readonly class='total' /> 
 
     </td> 
 
     <td width='8%'> 
 
      <input size='7' type='text' id='total_inr[]' name='total_inr[]' value='' /> 
 
     </td> 
 
    </tr> 
 
    </table> 
 
    </div> 
 
    <br> 
 
    <br>INR Value -> 
 
    <input type="text" class="inrvalue" id="inrvalue" name="inrvalue" value="65.831111"> 
 
    <br> 
 
    <br> 
 
    <table> 
 
    <td> 
 
     <input type='submit' value='--Save Data--' /> 
 
    </td> 
 
    </tr> 
 
    </table> 
 
</form>

+0

http://jsfiddle.net/arunpjohny/xdrLm5ao/ –

+0

Thanx很多Arun ..花時間。它現在工作正常。 –

+0

你在HTML部分改變了什麼吧? –

0

我建議你使用AngularJS編號:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="" ng-init="quantity=1;cost=5"> 
 
    <p>Total in dollar: {{ quantity * cost }}</p> 
 
</div>

+0

我還沒有在Angular Js工作過。所以不確定。 –

+0

AngularJS要容易得多,它需要時間來做出這樣的代碼 –