0

我目前正在使用这个 javscript 和 PHP 代码

<script>
function add(total)
{
    form2.thetotal.value = document.forms["form1" + total].total.value;
}
</script>

<form name="form2">
<table width="800" border="0" cellspacing="0" cellpadding="10" style="position:fixed; z-index:-999; background-color:#FFF;">
  <tr bgcolor="#eeeeee">
    <td>&nbsp;</td>
    <td colspan="2" width="50%"><strong>Total: </strong><input type="text" name="thetotal" id="thetotal" size="20" value="0" /></td>
    <td colspan="2" width="50%"><strong>VAT:</strong> </td>
  </tr>
  <tr bgcolor="#eeeeee">
    <td width="5%">&nbsp;</td>
    <td width="20%"><strong>Invoice Number</strong></td>
    <td width="35%"><strong>Company</strong></td>
    <td width="20%"><strong>Date</strong></td>
    <td width="20%"><strong>Total</strong></td>
  </tr>
  </table>
</form>

<form name="form1">
<table width="800" border="0" cellspacing="0" cellpadding="10">
<?php
$sql="SELECT * from billing_pdf_archive order by invoice_number ASC ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
    $counter++;
    $sql2="SELECT * from customer where sequence = '".$result["customer_sequence"]."' ";
    $rs2=mysql_query($sql2,$conn) or die(mysql_error());
    $result2=mysql_fetch_array($rs2);

    $sql3="SELECT * from reseller where sequence = '".$result["reseller_sequence"]."' ";
    $rs3=mysql_query($sql3,$conn) or die(mysql_error());
    $result3=mysql_fetch_array($rs3);

    if($result["customer_sequence"] != '0')
    {
        $company = $result2["company"];
    }
    elseif($result["reseller_sequence"] != '0')
    {
        $company = '<strong>Reseller: </strong>'.$result3["company"];
    }

    $total = $result["total"];

    echo '<tr>
    <td width="5%"><input type="checkbox" name="check" id="check" onclick=\'add('.$total.');\' /></td>
    <td width="20%">'.$result["invoice_number"].'</td>
    <td width="35%">'.$company.'</td>
    <td width="20%">'.$result["datetime"].'</td>
    <td width="20%">&pound;'.$result["total"].'</td>
  </tr>';
}
?>
</table>
</form>

所以你可以看到它是从 MySQL 数据库中选择的,我正在尝试这样做,所以当其中一个复选框被勾选时,它会将总数添加到“thetotal”文本字段(在表格 2 中)但它只是离开那个框为零 - 关于我能做什么的任何想法?

4

2 回答 2

0

这是你可以做到的(假设总数是int而不是float):

<script>
function add(total, this_chk_bx)
{
    //(if its float, use `parseFloat` instead of `parseInt`)

    if(this_chk_bx.checked==true){
    //add if its checked
    var tot_1 = parseInt(form2.thetotal.value);
    form2.thetotal.value = tot_1+parseInt(total);
    }
    else{
    //subtract if its unchecked
    }
}
</script>

.

在您的 php 中,您必须像这样发送复选框: onclick=\'add('.$total.', this);\'

.

于 2013-04-10T14:47:33.040 回答
0

您没有为表单 1 指定总字段,然后它引发了 javascript 错误,它不会工作。一探究竟

                            <script>
            function add(total)
            {
                form2.thetotal.value =  total + parseFloat(document.form1.formtotal.value);
            }
            </script>


            <form name="form1">

            <!-- replace field name formtotal with anything that you want-->
            <!-- replace field value with anything that you get from your mysql result-->
            <input type="text" name="formtotal" id="formtotal" value="10" />
            </form>
于 2013-04-10T14:53:10.410 回答