2017-07-18 132 views
1

我在代碼中遇到問題,不知道爲什麼會發生這種情況。我正在使用多個函數調用onchange事件在JavaScript中。當用戶將值輸入到字段中時,其他值由javascript函數通過onchange事件更改。但問題是,我在onchange事件中使用了唯一的函數,那麼它對於計算而言工作正常。 另一方面,其中多個函數用於onchange事件,然後這兩個函數隨機調用。我需要防止這種手段,我只想要那個單一的函數調用,這是需要的。 爲什麼這兩個函數調用,以及如何根據需要分別調用它們?如何防止在javascript中自動多次調用onchange函數?

function update_net_amount(){ 
 
\t \t var moduleprice = $('#module').val(); 
 
\t \t var charges = $('#charges').val(); 
 
\t \t var amount1 = ((parseInt(moduleprice) + parseInt(charges))); 
 
\t \t var discount1 = $('#discount').val(); 
 
\t \t var netamount1 = amount1 - discount1; 
 
\t \t $('#netamount1').val(netamount1); 
 
\t \t alert("amc1==="+netamount1); 
 
\t \t } 
 
\t function update_net_amount_percentage(){ 
 
\t \t var moduleprice = $('#module').val(); 
 
\t \t var charges = $('#charges').val(); 
 
\t \t var discount = $('#discount1').val(); 
 
\t \t var amount1 = (parseInt(moduleprice) + parseInt(charges)); 
 
\t \t var netamount = ((amount1 * discount)/100); 
 
\t \t var total = amount1 - netamount; 
 
     $('#netamount1').val(total); 
 
\t \t alert("amc2==="+total); 
 
\t } 
 
\t function update_net_amount_percenatge_amount(){ 
 
\t \t var moduleprice = $('#module').val(); 
 
\t \t var charges = $('#amcpercent').val(); 
 
\t \t var netamount = ((moduleprice * charges)/100); 
 
\t \t var amount2 = (parseInt(moduleprice) + parseInt(netamount)); 
 
\t \t var discount = $('#discount').val(); 
 
\t \t var nettotal = amount2 - discount; 
 
     $('#netamount1').val(nettotal); 
 
\t \t alert("amc3==="+nettotal); 
 
\t } 
 
\t function update_net_amount_percenatge_amount_percentage(){ 
 
\t \t var moduleprice = $('#module').val(); 
 
\t \t var charges = $('#amcpercent').val(); 
 
\t \t var netamounts = ((moduleprice * charges)/100); 
 
\t \t var amount3 = (parseInt(moduleprice) + parseInt(netamounts)); 
 
\t \t var discounts = $('#discount1').val(); 
 
\t \t var disamount = ((amount3 * discounts)/100); 
 
\t \t var fulltotal = amount3 - disamount; 
 
\t \t $('#netamount1').val(fulltotal); 
 
\t \t alert("amc4==="+fulltotal); 
 
\t \t /* discount in % subtracts from amount3 */ 
 
\t }
<html> 
 
<div class="pro-add"> 
 
      <form action="" method="post" enctype="multipart/form-data" id="name"> 
 
      \t \t <label>Project</label> 
 
        <span class="txt-val"><input type="text" name="pro" value="<?php echo $data['fvprojectname']; //from database ?>" disabled="disabled" /></span> 
 
\t \t \t \t <label>Module</label> 
 
       <span class="txt-val"><input type="text" name="mod" value="<?php echo $data['fvmodulename']; //from database?>" disabled="disabled" /></span><br /> 
 
      \t \t <label>AMC</label> 
 
        <span class="txt-val amc"><input type="text" id="charges" onchange="javascript:update_net_amount();" name="charges" placeholder="amount in rupees only." value="0" /></span> 
 
\t \t \t \t \t <label>or</label> 
 
        <span class="txt-val amc-per"><input type="text" name="percentcharges" id="amcpercent" onchange="javascript:update_net_amount_percenatge_amount();" placeholder="in %" />&nbsp;%</span> 
 
\t \t \t \t  
 
\t \t \t \t <div class="invoice-table"> 
 
         <table style="border-collapse: collapse; border: 1px solid black; text-align:center; width:130%;"> 
 
          <tr> 
 
\t       <td rowspan="2" class="col-all" style="border: 1px solid black; background-color: #00701c; color: #fff; font-weight:bold;"> 
 
           Sr. No.</td> 
 
           <td rowspan="2" class="col-all" style="border: 1px solid black; background-color: #00701c; color: #fff; font-weight:bold;"> \t \t \t         Price Per Module</td> 
 
           <th colspan="3" class="col-all" style="border: 1px solid black; background-color: #00701c; color: #fff; font-weight:bold;"> 
 
           Amount</th> 
 
          </tr> 
 
          <tr> 
 
           <td colspan="2" class="col-all" style="border: 1px solid black; background-color: #00701c; color: #fff; font-weight:bold;"> 
 
           Discount</td> 
 
           <td colspan="2" class="col-all" style="border: 0px solid black; background-color: #00701c; color: #fff; font-weight:bold;"> 
 
           Net Amount</td> 
 
          </tr> 
 
          <?php global $counter; $counter=1; ?> 
 
          <tr> 
 
\t       \t <td rowspan="2" style="border: 0px solid black;"><?php echo $counter++; ?></td> 
 
          </tr> 
 
          <tr> 
 
          
 
          <td style="border-collapse: collapse; border: 1px solid black;"><input type="text" id="module" name="price" placeholder="module price." value="<?php echo $data['fiamount']; //from database?>" onchange="javascript:update_net_amount();" /></td> 
 
           <td colspan="2" style="border-collapse: collapse; border: 1px solid black;"><input type="text" id="discount" onchange="javascript:update_net_amount();update_net_amount_percenatge_amount();" name="discount" class="set-tr" placeholder="enter discount in rupees." /> or&nbsp;&nbsp; 
 
           <input type="text" id="discount1" onchange="javascript:update_net_amount_percentage();update_net_amount_percenatge_amount_percentage();" name="perdiscount" class="set-tr-per" placeholder="enter discount in %." /></td> 
 
           <td colspan="2" style="border-collapse: collapse; border: 1px solid black;"><input type="text" id="netamount1" name="net" class="set-tr-net" placeholder="net amount in rupees." value="<?php //echo $data['fiamount']; ?>" onchange="javascript:update_net_amount_percenatge_amount();" /></td> 
 
          </tr> 
 
         </table> 
 
        </div> 
 
       
 
        <br/> 
 
        <span class="btn-adding"><input type="submit" name="adding" value="Save" /></span> 
 
        <span class="btn-cancel"><input type="submit" name="cancel" value="Cancel" /></span> 
 
      </form> 
 
     </div> 
 
</html>

如何調用函數調用需要在JavaScript中分別onchange事件。

在此先感謝,請幫助我。

回答

1

不要調用所有函數內聯,在js文件中寫入您的事件代碼。

例子:

$('#charges').change(function(){ 
    update_net_amount(); 
}) 

$('#discount1').change(function(){ 
    update_net_amount_percentage(); 
    update_net_amount_percenatge_amount_percentage(); 
}) 

從HTML刪除的onchange事件:

<input type="text" id="discount1" name="perdiscount" class="set-tr-per" placeholder="enter discount in %." /> 
+0

先生我是新來的js。你會幫我怎麼把我的函數放在一個單獨的文件中,以及如何在html中調用他們的onchange事件 請給我一個例子。 –

+0

如何使用一個單獨的文件中使用此 $( '#費')變化(函數(){ update_net_amount(); }) $( '#discount1')變化(函數(){。 update_net_amount_percentage(); update_net_amount_percenatge_amount_percentage(); }) 我已經使用過這個功能,但沒有從一個單獨的文件調用函數。如何執行該操作。請幫助先生。 –

+0

你把你的功能放在哪裏?然後放'。 $('#charges')。change(function(){update_net_amount();})$('#discount1')。change(function(){update_net_amount_percentage(); update_net_amount_percenatge_amount_percentage();})'那裏也有 –

0

您可以鏈接該功能。例如:

$('selector').on('change', function(){ 
    firstFunction(); 
} 

function firstFunction(){ 
//do your stuff 
    secondFunction(); 
} 
+0

親愛的你會清楚你的解決方案,因爲我是新來的JS。 如何使用您的功能我的代碼從情況$( '選擇')走出上( '變',函數(){ 了firstFunction();} 。 功能了firstFunction(){// 做你的東西 secondFunction(); } –