在while循环中切换大小写(JavaScript)(PHP)

时间:2015-06-10 22:58:19

标签: javascript while-loop switch-statement

我有这个JavaScript代码,它运行良好,但是当我尝试基于$id_math自定义一些数学运算时,它只读取while循环内数组的第一行,所以使用它来计算所有行相同的$id_math,我在代码中需要做哪些更改,为每一行获取单独的$id_math并执行相应的操作?

$id_math是一个php变量,用于存储我从数据库中获取的值。 例如:$id_math = 11$operation_name = addition

Here is my example

脚本

$(function(){

    CalculateTotal();
    // Adding the change events for the Price and 
    // quantity fields only..
    // Changing the total should not affect anything
    $('.quantity , .price').on('change', function() {
        UpdateTotals(this);
    });
});

function UpdateTotals(elem) {
    // This will give the tr of the Element Which was changed
    //in my code I have it like this 
   var abc=<?php echo id_math ?>;                   
    var $container = $(elem).parent().parent();
    var quantity = $container.find('.quantity').val();
    var price = $container.find('.price').val();
    var subtotal = parseFloat(quantity) + parseFloat(price);
    var subtotalmultiplication = parseFloat(quantity) * parseFloat(price);
    var subtotaldivision = parseFloat(quantity) / parseFloat(price);

    switch (abc) {
    case 12:
         $container.find('.subtotal').text(subtotal.toFixed(2));
    $container.find('.txtresult').val(subtotal.toFixed(2));
        break;
    case 11:
       $container.find('.subtotal').text(subtotalmultiplication.toFixed(2));
    $container.find('.txtresult').val(subtotalmultiplication.toFixed(2));
        break;
    case 13:
       $container.find('.subtotal').text(subtotaldivision.toFixed(2));
    $container.find('.txtresult').val(subtotaldivision.toFixed(2));
        break;

}

   //document.getElementById("txtresult").value = subtotal.toFixed(2);

}

function CalculateTotal(){
  // This will Itearate thru the subtotals and 
    // claculate the grandTotal and Quantity here

    var lineTotals = $('.subtotal');
    var quantityTotal = $('.quantity');
    var grandTotal = 0.0;
    var totalQuantity = 0;
    $.each(lineTotals, function(i){
        grandTotal += parseFloat($(lineTotals[i]).text()) ;
        totalQuantity += parseFloat($(quantityTotal[i]).val()) 
    });

    $('.totalquantity').text(totalQuantity);
    $('.grandtotal').text(parseFloat(grandTotal ).toFixed(2) );       

} 

php代码

<table id="tabla-registros" class="table table-striped table-bordered bootstrap-datatable datatable responsive">

<thead>
<tr>
     <th>Nº Reg</th>
     <th>Fecha</th>
     <th>Nombre Completo del Paciente</th>
     <th>Clave para la Descarga</th>
     <th>Estado del Registro</th>
     <th>Acciones</th>

</tr>
</thead>
<tbody>
    <?php

            $numrows = mysql_num_rows($list_results);

            if($numrows > 0){

                while($row=mysql_fetch_array($list_results)){
                $patient_name= utf8_encode($row['nombrepaciente']);
                $keyword= $row['llave'];
                $url_result= $row['resultado'];
                $comments= $row['observaciones'];
                $status_result= $row['nombre_estado'];
                $date_result= $row['fecha'];
                $id_status= $row['idestado'];
                $id_resultado= $row['idresultados'];
                $id_math = $row['idmath'];

                ?>
 <tr>
       <td><?php echo $id_resultado;?></td>
       <td><?php echo $date_result;?></td>
       <td><?php echo $patient_name;?></td>
       <td><?php echo $keyword;?></td>

            <?php switch($id_status){ 

                    case '3':?>
                        <td><span class="label-success label label-default"><?php echo $status_result; ?></span></td>
                            <?php  break; ?>

                        <?php   case '1':?>
                            <td>  <span class="label-warning label label-default"><?php echo $status_result; ?></span></td>
                        <?php  break;?>


                            <?php   case '2':?>
                            <td> <span class="label-default label label-danger"><?php echo $status_result; ?></span></td>
                        <?php  break; }?>

     <!--This is the block of the code where I need help because the javascript code only reads the first row of the while loop to perform the operation, but the second row has another id_math to calculate other operations -->
                       <table>
                           <tr>
                               <th width=200px>Description</th> 
                               <th width=60px>DATA1</th> 
                               <th width=60px>DATA2</th> 
                               <th width=60px>Total</th> 
                           </tr>
                            <tr>
                               <td width=200px>CALCULATE</td> 
                              <td width=60px><input type="text" value="15.99" class="price" /></td> 
                              <td width=60px><input type="text" value="1" class="quantity" /></td> 
                              <td width=60px><input type="text" value="" name="txtresult"  id= "txtresult" class="txtresult" /></td> 

                          </tr> 



                           </table>

0 个答案:

没有答案