php - How to create a condition in jquery onchange event? -


i need problem , sorry question problem don't know how title question. hope understand problem. here is.

by way creating purchase order form.

i created table form consist of textboxes, selection , buttons. scenario is. have selection box contains supplier names. , after selection made. auto filter activated. filling supplier's information/detail. such address, contact number, tin, etc...

below supplier's information have rows inserting orders. first columns item code second quantity , third price , last price total. below table have textboxes compute grand total, vat, discount, etc etc...

now here's part don't know how do. after supplier selection, jquery must detect if there's value in tin textbox. in displaying value automatically don't have problem that. real problem in computation side. because have said earlier below table computation vat. computation vat must triggered if tin textbox not empty. meaning if tin textbox not empty computation vat should enable. if empty, jquery should skip part.

here's sample code:

jquery part

    //automatic computation in rows     $('[id^=qty],[id^=price]').on('change',function() {          var index = this.id.match(/\d+/)[0];         var qty = parseint($('#qty'+index).val());         var price = parsefloat($('#price'+index).val());         var total = 0;          $('#total'+index).val((qty * price ? qty * price : 0).tofixed(2));          var total = 0;         $('[id^=total]').each(function(index){             total+=parsefloat($(this).val()?$(this).val():0);         });         $('#sum_of_total').val(total.tofixed(2));          //now here's computation vat should skip if tin empty         var vatable = total / 1.12;         var vatable_amt = vatable * 0.12;         var totalvat = vatable + vatable_amt;          $('#vatable').val(vatable.tofixed(2));          $("#vatable_amount").val(vatable_amt.tofixed(2));         $("#gtotal").val(totalvat.tofixed(2));         //end of computation     });      //here's auto assign of values     //auto assign supplier info     $('#supplier_list').bind('change', function(){         var var_add_category ='<?php echo site_url("purchaseorder_controller/supplier_details"); ?>';         $.ajax({             type:'post',             url: var_add_category,             data:{ id: $(this).val() },             datatype:'json',             success:function(d){                  var bankname = d['bankname'];                 var bankbranch = d['bankbranch'];                  $("[name=spaddress]").val(d['spaddr']);                 $("[name=tin]").val(d['sptinno']);                 $("[name=contactperson]").val(d['pricontactname']);                 $("[name=contactnumber]").val(d['sptelno']);                 $("[name=bank]").val(bankname + ' - ' + bankbranch);                 $("[name=account_name]").val(d['bankacctname']);                 $("[name=account_no]").val(d['bankacctno']);              }         });         }); 

here's html/php part:

here's supplier entry, trigger add information in supplier fields  <select id="supplier_list" style="width: 100%;" name="supplier_id" required="required">     <option value="">--choose supplier--</option>       <?php          foreach($resultselectsupplier->result_array() $suppliers){            echo "<option value=".$suppliers['spid'].">".$suppliers['spname']."</option>";          }       ?> </select> . . . //the tin textbox need validate if empty or not(used computation in vat)  <td>       <label>tin #</label>  </td>  <td>       <input type="text" class="k-textbox" name="tin" maxlength="11" id="tin_number" />  </td> 

here's php part looping rows adding order item

 <?php      $sqlgetcode = "select itemid,itemcode,itemname items";     $resultgetcode = $this->db->query($sqlgetcode);      for($i = 1; $i < 16; $i++){         echo "<tr>";           echo "<td>";              echo "<select name='code[]' id='code{$i}' style='width:100'>";                 echo "<option value=''><label>--choose items--</label></option>";                  foreach($resultgetcode->result_array() $list){                    echo "<option value='".$list['itemid']."'>".$list['itemcode']." --- ".$list['itemname']."</option>";                   }              echo "</select>";           echo "</td>";            echo "<td><input type='text' name='qty[]' id='qty{$i}' style='text-align: center' value='' /></td>";           echo "<td><input type='text' name='price[]' id='price{$i}' style='text-align: right;' value='' onblur='' /></td>";           echo "<td><input type='text' name='total[]' id='total{$i}' style='font-family: courier; text-align: right; background-color: lightgray; color: red' readonly='readonly' value='' /></td>";       echo "<tr>";      } 

?>

the textbox show computed items

 <div align="right">             <table>                     <tr>                         <td></td>                         <td></td>                         <td>vatable amount:</td>                         <td><input type="text" class="k-textbox" value="0.00" readonly="readonly" style="color: red; text-align: right; font-family: courier" name="vatable" id="vatable" /></td>                     </tr>                      <tr>                         <td></td>                         <td></td>                         <td>vat input:</td>                         <td><input type="text" class="k-textbox" value="0.00" readonly="readonly" style="color: red; text-align: right; font-family: courier" name="vatable_amount" id="vatable_amount" /></td>                     </tr>                      <tr>                         <td></td>                         <td></td>                         <td>total sales:</td>                         <td><input type="text" class="k-textbox" value="0.00" readonly="readonly" style="color: red; text-align: right; font-family: courier" name="subtotal" id="gtotal" /></td>                     </tr>                      <tr>                         <td></td>                         <td></td>                         <td>purchase discount:</td>                         <td><input type="text" class="k-textbox" value="0.00" style="color: red; text-align: right; font-family: courier" name="discount" id="discount" /></td>                     </tr>                      <tr>                         <td></td>                         <td></td>                         <td>total amount due:</td>                         <td><input type="text" class="k-textbox" value="0.00" style="color: red; text-align: right; font-family: courier" name="total_amt_due" id="total_amt_due" /></td>                     </tr>                     <input type="hidden" id="sum_of_total" name="sum_of_total" />             </table>         </div> 

here's formula computation:

*note: computaion trigger if tin textbox not empty

vatable amount = sum of total / 1.12 vatable input = vatable amount * 0.12 total sales = vatable amount + vatable input 

*note: computation fixed, if no tin sum of total default value computing total amount due

computation total amount due: purchase discount = user input discount number here total amount due = total sales - purchase discount (should trigger automatically) 

here's fiddle: http://jsfiddle.net/rochellecanale/dtfv4/

you may add condition in onchange event:

//now here's computation vat should skip if tin empty var vatable = 0; var vatable_amt = 0; var totalvat = 0;  if($("#tin_number").val().length != 0) {     var vatable = total / 1.12;     var vatable_amt = vatable * 0.12; } totalvat = vatable + vatable_amt;  $('#vatable').val(vatable.tofixed(2));  $("#vatable_amount").val(vatable_amt.tofixed(2)); $("#gtotal").val(totalvat.tofixed(2)); //end of computation 

calculate vat when length of tin no. greater 0. ideally length should equal 10 , may check if tin no. number.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -