2015-05-08 33 views
0

我想要執行一些計算並在div標籤中打印值。如何在jQuery中進行計算和打印值?

我想計算總和sum1 = (total * total2) + jsvar1;sum2 = (total1 * total2) + jsvar1;

接下來,我想在html div標記中顯示sum1sum2值。

我怎麼能用我所擁有的完成這項工作?如果有人能幫我做到這一點,我將非常感激。非常感謝你。


這是我到目前爲止有:

jsvar1 = <?php 
    $sql9 = mysql_query("select room_rate from room_category where hotel_id='".$hotel_id."' and room_type = '".$var_value."' "); 
    $res9 = mysql_fetch_array($sql9); 
    $roomrate = $res9["room_rate"]; 
    echo $roomrate; 
?>; 
document.write(jsvar1); 


$(document).ready(function() { 
    $('#roomOptions #select1').change(function() { 
     var total = 0; 
     $('#roomOptions #select1').each(function() { 
      total+=parseInt($(this).val()); 

     }); 
     $('#roomOptions #roomOptions_total').html(total); 
    }); 


    $('#roomOptions #select2').change(function() { 
     var total1 = 0; 
     $('#roomOptions #select2').each(function() { 
      total1+=parseInt($(this).val()); 

     }); 
     $('#roomOptions #roomOptions_total1').html(total1); 
    }); 



    $('#roomOptions #select3').change(function() { 
     var total2 = 0; 
     $('#roomOptions #select3').each(function() { 
      total2+=parseInt($(this).val()); 

     }); 
     $('#roomOptions #roomOptions_total2').html(total2); 
    }); 


}); 
+0

我想知道如何計算這些值? –

回答

1

需要聲明改變事​​件的外面,在這種情況下你的總變量,你計算的總和:

$(document).ready(function() { 
    var total, total1, total2; 
    $('#roomOptions #select1').change(function() { 
     total = 0; 
     $('#roomOptions #select1').each(function() { 
      total+=parseInt($(this).val()); 

     }); 
     $('#roomOptions #roomOptions_total').html(total); 
     calcualteSum1(); 
    }); 
    $('#roomOptions #select2').change(function() { 
     total1 = 0; 
     $('#roomOptions #select2').each(function() { 
      total1+=parseInt($(this).val()); 

     }); 
     $('#roomOptions #roomOptions_total1').html(total1); 
     calcualteSum2(); 
    }); 

    $('#roomOptions #select3').change(function() { 
     total2 = 0; 
       $('#roomOptions #select3').each(function() { 
      total2+=parseInt($(this).val()); 

     }); 
     $('#roomOptions #roomOptions_total2').html(total2); 
     calcualteSum1(); 
     calcualteSum2(); 
    }); 

    function calcualteSum1() { 
     $('#sum').html((total * total2) + jsvar1); 
    } 
    function calcualteSum2() { 
     $('#sum').html((total1 * total2) + jsvar1); 
    } 
}); 
+0

非常感謝。這是工作.. –