HTML表单中的计算字段无效

时间:2014-03-28 15:33:05

标签: javascript jquery html5

我正在使用DreamWeaver,PHP,MySQL,JQuery Mobile和JavaScript。我在表上显示MySQL记录,每行都有一个编辑链接来打开更新记录文件。在插入记录文件中使用JavaScript,我可以使用JavaScript函数计算字段值,但现在,我正在尝试在更新记录文件中执行相同的操作。

我现在遇到的问题是,在第二个文件中调用了JavaScript函数,但是进行了任何计算。

我看到的唯一区别是在第二个文件(更新记录)上的字段已经有了值,但我不确定这是否是问题的根源,但我猜不是,那么用户可以手动改变价值观。

这是两个文件的JavaScript代码:

<script type="text/javascript"> 
function calculatetotal(){
    alert("I am an alert box!");
    var mat = 0;
    mat = parseFloat(document.getElementById('mat').value);
    var mo = 0;
    mo = parseFloat(document.getElementById('mo').value);
    var uti = 0;
    uti = parseFloat(document.getElementById('uti').value);
    var ind = 0;
    ind = parseFloat(document.getElementById('ind').value);
    var poriva = 0;
    poriva = parseFloat(document.getElementById('poriva').value);
    var  totaliva =0;
    var totcliente =0;
    totcliente = (mat+mo+uti+ind).toFixed(2);;
    var totaliva = ((poriva)/100)*totcliente;
    document.getElementById('totCliente').value = (mat+mo+uti+ind).toFixed(2);
    document.getElementById('totaliva').value = totaliva; 
    document.getElementById('totalpre').value= parseFloat(totaliva)+parseFloat(totcliente);
}
</script>

我需要你的帮助才能理解为什么它在插入记录文件上工作而不在更新记录文件上。

这是来自更新记录部分的HTML表单部分的代码:

  <form method="post" name="form1" id="form1" data-ajax="false" action="<?php echo $editFormAction; ?>">
            <table align="center">
              <tr valign="baseline">
                <td nowrap align="right">Obra:</td>
                <td><select name="int_obra">
                  <?php 
do {  
?>
                  <option value="<?php echo $row_Recordset2['idObra']?>" <?php if (!(strcmp($row_Recordset2['idObra'], htmlentities($row_Recordset1['int_obra'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>><?php echo $row_Recordset2['nombreObra']?></option>
                  <?php
} while ($row_Recordset2 = mysql_fetch_assoc($Recordset2));
?>
                </select></td>
              <tr>
              <tr valign="baseline">
                <td nowrap align="right">Materiales:</td>
                <td><input type="text" name="dbl_materiales" value="<?php echo htmlentities($row_Recordset1['dbl_materiales'], ENT_COMPAT, 'UTF-8'); ?>" size="32"   id="mat"  onChange="calculatetotal()"></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">Mano de Obra:</td>
                <td><input type="text" name="dbl_mano_de_obra" value="<?php echo htmlentities($row_Recordset1['dbl_mano_de_obra'], ENT_COMPAT, 'UTF-8'); ?>" size="32" id="mo" onChange="calculatetotal()"></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">Utilidad:</td>
                <td><input type="text" name="dbl_utilidad" value="<?php echo htmlentities($row_Recordset1['dbl_utilidad'], ENT_COMPAT, 'UTF-8'); ?>" size="32" id="uti" onChange="calculatetotal()"></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">Indirectos:</td>
                <td><input type="text" name="dbl_indirectos" value="<?php echo htmlentities($row_Recordset1['dbl_indirectos'], ENT_COMPAT, 'UTF-8'); ?>" id="ind" onChange="calculatetotal()"size="32"></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">Pre. aprobado por cliente:</td>
                <td><input name="dbl_total" type="text" id="totcliente"  value="<?php echo htmlentities($row_Recordset1['dbl_total'], ENT_COMPAT, 'UTF-8'); ?>" size="32" readonly></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">% IVA:</td>
                <td><input type="text" name="dbl_porcentaje_iva" value="<?php echo htmlentities($row_Recordset1['dbl_porcentaje_iva'], ENT_COMPAT, 'UTF-8'); ?>" size="32" id="poriva" onChange="calculatetotal()"></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">Total IVA:</td>
                <td><input name="dbl_total_iva" type="text" value="<?php echo htmlentities($row_Recordset1['dbl_total_iva'], ENT_COMPAT, 'UTF-8'); ?>" size="32" id="totaliva" readonly></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">TOTAL DEL PRESUPUESTO:</td>
                <td><strong>
                <input name="dbl_total_presupuesto" type="text" value="<?php echo htmlentities($row_Recordset1['dbl_total_presupuesto'], ENT_COMPAT, 'UTF-8'); ?>" size="32" id="totalpre" readonly>
                </strong></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">MONEDA:</td>
                <td><select name="int_moneda">
                  <option value="1" <?php if (!(strcmp(1, htmlentities($row_Recordset1['int_moneda'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>>PESO MXN</option>
                  <option value="2" <?php if (!(strcmp(2, htmlentities($row_Recordset1['int_moneda'], ENT_COMPAT, 'UTF-8')))) {echo "SELECTED";} ?>>USD</option>
                </select></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">&nbsp;</td>
                <td><input type="submit" value="Actualizar Presupuesto"></td>
              </tr>
            </table>
            <input type="hidden" name="MM_update" value="form1">
            <input type="hidden" name="idPresupuesto" value="<?php echo $row_Recordset1['idPresupuesto']; ?>">
          </form>

2 个答案:

答案 0 :(得分:0)

我已经更新了JavaScript代码,现在它按预期工作了。谢谢你的帮助。

<script type="text/javascript"> 
function calculatetotal(){

    var mat = 0;
    mat = parseFloat(document.getElementById('mat').value);
    var mo = 0;
    mo = parseFloat(document.getElementById('mo').value);
    var uti = 0;
    uti = parseFloat(document.getElementById('uti').value);
    var ind = 0;
    ind = parseFloat(document.getElementById('ind').value);
    var poriva = 0;
    poriva = parseFloat(document.getElementById('poriva').value);

    totcliente = (mat+mo+uti+ind).toFixed(2);
    var totaliva = parseFloat(((poriva)/100)*totcliente);
    document.getElementById('totcliente').value = totcliente;
    document.getElementById('totaliva').value = totaliva.toFixed(2);
    document.getElementById('totalpre').value = (parseFloat(totcliente)+parseFloat(totaliva)).toFixed(2);
}
</script>

答案 1 :(得分:-1)

</body>之前将脚本添加到页面末尾juste并调用函数:

<script type="text/javascript"> 
function calculatetotal(){
    alert("I am an alert box!");
    var mat = 0;
    mat = parseFloat(document.getElementById('mat').value);
    var mo = 0;
    mo = parseFloat(document.getElementById('mo').value);
    var uti = 0;
    uti = parseFloat(document.getElementById('uti').value);
    var ind = 0;
    ind = parseFloat(document.getElementById('ind').value);
    var poriva = 0;
    poriva = parseFloat(document.getElementById('poriva').value);
    var  totaliva =0;
    var totcliente =0;
    totcliente = (mat+mo+uti+ind).toFixed(2);;
    var totaliva = ((poriva)/100)*totcliente;
    document.getElementById('totCliente').value = (mat+mo+uti+ind).toFixed(2);
    document.getElementById('totaliva').value = totaliva; 
    document.getElementById('totalpre').value= parseFloat(totaliva)+parseFloat(totcliente);
}

calculatetotal();

</script>
</body>
</html>