2015-12-28 80 views
3

沒有馬瑟我寫在輸入字段什麼,我只甚至從頁面加載,我得到NaN的的Javascript總和字段錯誤的NaN

任何想法,爲什麼得到喃?我需要數量*以價格並給出結果,這裏是代碼。我將顯示現在的代碼

<form action="?F=save-sale" method="post" name="venta"> 
<table class="table-global"> 
     <tr> 
     <td class="table-global-td-title">Cantidad 

     </td> 
     <td class="table-global-td-title">Precio venta</td> 
     <td class="table-global-td-title">Vendedor</td> 
     <td class="table-global-td-title">Documento</td> 

     <td class="table-global-td-title">M&eacute;todo de pago</td> 
     <td class="table-global-td-title">Suma total</td> 
     <td class="table-global-td-title"></td> 
     </tr> 
     <tr> 
     <td class="table-global-td-subtitle"><input type="number" class="input-global-100" name="cantidad" id="cantidad">   </td> 
      <td class="table-global-td-subtitle"><input type="number" class="input-global-100" name="venta" id="venta" value="0"></td> 
      <td class="table-global-td-subtitle"> 
      <select style="text-transform:capitalize" class="select-global-120" name="vendedor" id="vendedor"> 

    <option value="" selected>Seleccionar</option> 
    <option value="001">001</option> 

</select>   </td> 
     <td class="table-global-td-subtitle"> <select class="select-global-132" name="comprobante" id="comprobante"> 


      <option value="Boleta" selected>Boleta</option>   
      <option value="Factura">Factura</option> 


      </select>  </td> 
     <td class="table-global-td-subtitle"><select class="select-global-120" name="metodo" id="metodo" > 
      <option value="Transferencia">Transferencia</option> 

      <option value="Efectivo" selected>Efectivo</option> 
      <option value="Cheque">Cheque</option> 
      <option value="Transbank">Transbank</option> 



     </select></td> 
     <td class="table-global-td-subtitle"> 

      <input type="text" class="input-global-total-100" name="ventatotal" id="ventatotal" readonly value="0" />  </td> 
     <td class="table-global-td-subtitle"> 
     <input class="submit-global-120" type="submit" value="Realizar la venta" /></td> 
     </tr> 
     </table> 

<script> 
var aacosto = document.getElementsByName('costo')[0]; 
var aacostototal = document.getElementsByName('costototal')[0]; 
var aaventa  = document.getElementsByName('venta')[0]; 
var aaventatotal = document.getElementsByName('ventatotal')[0]; 
var aacantidad = document.getElementsByName('cantidad')[0]; 
var aaganancia = document.getElementsByName('ganancia')[0]; 
var aagananciatotal = document.getElementsByName('gananciatotal')[0]; 

function updateInput() { 
aaventatotal.value = parseFloat(aaventa.value) * parseFloat(aacantidad.value); 

} 

aaventa.addEventListener('keyup', updateInput); 
aaventatotal.addEventListener('change', updateInput); 
aacantidad.addEventListener('keyup', updateInput); 
updateInput(); 
</script> 
</form> 

這裏是一個小提琴所以你們可以看到它的工作

https://fiddle.jshell.net/v6spxoqv/10/

+0

是您的腳本在''元素的底部?如果沒有,請將它移動到那裏,或者將所有'var'語句放入函數_中。 – Xufox

+0

沒有在HTML表格裏面的代碼中間 – Locatarios

+0

但是不在輸入字段下?然後HTML元素尚未加載。只需將腳本放在HTML表單的輸入框中即可。 – Xufox

回答

2

只要有計算產品之前,這個條件if (aaventa.value && aacantidad.value)。它確保值不是。其他一切都很好。

var aaventa = document.getElementsByName('venta')[0]; 
 
var aaventatotal = document.getElementsByName('ventatotal')[0]; 
 
var aacantidad = document.getElementsByName('cantidad')[0]; 
 

 
function updateInput() { 
 
    if (aaventa.value && aacantidad.value) 
 
    aaventatotal.value = parseFloat(aaventa.value) * parseFloat(aacantidad.value); 
 
    else 
 
    aaventatotal.value = 0; 
 

 
} 
 

 
aaventa.addEventListener('keyup', updateInput); 
 
aaventatotal.addEventListener('change', updateInput); 
 
aacantidad.addEventListener('keyup', updateInput); 
 
updateInput();
<input name="venta"> 
 
<input name="cantidad"> 
 
<input name="ventatotal">

+0

爲什麼這個downvote?謹慎解釋? – void

+0

@Xufox如果DOM中的元素不是*,那麼'element.value'會拋出一個錯誤。 – andlrc

+0

這將是一個痛苦,如果我需要更多的輸入更新... – Locatarios

0
<input id="venta"> 
<input id="cantidad"> 
<input id="ventatotal"> 

使用jQuery

$('#venta').change(function(){ updateInput() ;}); 
$('#cantidad').change(function(){ updateInput() ;}); 
$('#ventatotal').change(function(){ updateInput() ;});