Javascript Sum Fields错误NaN

时间:2015-12-28 15:51:59

标签: javascript php html css css3

我在输入字段中写的没有其他东西我只是从页面加载中得到NaN我得到了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/

2 个答案:

答案 0 :(得分:2)

在计算产品之前,只需拥有此条件<input name="venta"> <input name="cantidad"> <input name="ventatotal">。它确保值不是为空。其他一切都很好。

&#13;
&#13;
<html>
<body>
    <form action="video-up.php" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label>
        <input type="file" name="file" id="file" />
        <br>
        <input type="submit" name="submit" value="Submit" />
    </form>
</body>
</html> 
&#13;
<?php
$allowedExts = array("mov");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ( $_FILES["file"]["type"] == "video/quicktime" && in_array($extension, $allowedExts) )
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }   
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "upload/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
    }
}
else
{
    echo "Invalid file";
}

?>
<br><br>$_FILES
<pre>
<?php
    var_dump($_FILES);
?>
</pre> 
<br><br>$_POST
<pre>
<?php
    var_dump($_FILES);
?>
</pre> 
<br><br>$_GET
<pre>
<?php
    var_dump($_GET);
?>
</pre>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

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

使用jQuery

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