简体   繁体   English

如何用JavaScript计算金额?

[英]How to calculate amount by javascript?

As u can see i have this form that calculate number of employees multiply by test that will be select by user. 如您所见,我有此表格可以计算出雇员人数乘以将由用户选择的测试。 number of employees will be input by user and test are coming from database. 用户输入的员工数量,测试来自数据库。 each test have their individual price set in database. 每个测试都在数据库中设置了各自的价格。 I used script for dropdown list. 我使用脚本作为下拉列表。

If user put any value in no.of employees and check on anytest then in total amount sum should be calculate onclick buytest? 如果用户在员工人数中设置了任何值并进行了任何测试,则总金额应计算为onclick buytest吗?

<form action="add_corporate_test.php" method="POST">
    <script type="text/javascript">
        $(document).ready(function () {
            $('#example-onDeselectAll').multiselect({
                includeSelectAllOption: true,
                onDeselectAll: function () {
                    alert('please select any test!');
                }
            });
        });
    </script>
    <div class="row">
        <h5 class="cor_label">No. of Emp/Students:</h5>
        <input
            type="text" id="no_emp"
            placeholder="No. of Employees/Students"
            class="normal" required
        >
        <h5 class="cor_label">Email Address:</h5>
        <input
            type="text"
            id="no_emp2"
            placeholder="Email Address"
            class="normal" value="<?php echo $_SESSION[email]; ?>"
            required
            disabled
        >
        <h5 class="cor_label">Select Test:</h5>
        <select id="example-onDeselectAll" multiple="multiple">
            <?php
            $sql = mysql_query("select * from `test` ");
            while ($data = mysql_fetch_assoc($sql)) {
                $sum = 0;
                ?>
                <option value="<?php echo $data['test_name']; ?>">
                    <?php echo $data['test_name']; ?>
                </option>
            <?php } ?>
        </select>
        <h5 class="cor_label">Total Amount:</h5>
        <input
            type="text"
            id="no_emp3"
            class="normal"
            name="total_amount"
            placeholder="total amount"
            value="<?php echo $sum; ?>"
            disabled
        >
        <button class="btn btn-primary" type="submit" name="corporate_buy">
            Buy Test
        </button>
    </div>
</form>

get the test and value on select option. 在选择选项上获得测试和价值。 on select split the value using javascript. 选择使用javascript拆分值。 and then sum the amount and no.of.employee 然后将金额与员工人数相加

` `

<select id="example-onDeselectAll" multiple="multiple" >
            <?php   
                    $sql=mysql_query("select * from `test` ");
                while($data=mysql_fetch_assoc($sql))
                {
                    $sum=0; 
                ?>                                  
               <option  value="<?php echo $data['test_name'];?>_<?php echo $data['price'];?>"><?php echo $data['test_name'];?></option>                             
            <?php }?>                     
            </select>  

<script>
$('#example-onDeselectAll').change(function() {
var value = $(this).val();
var no_emp = $("#no_emp").val();
var res = value .split("_");
var tot_amount = res[1]*no_emp;
$("#no_emp3").val(tot_amount);
});
</scrit>    

` `

Hi bro, use for loop for script b'coz u have multiple select option ` 嗨,兄弟,为脚本b'coz使用for循环,您有多个选择选项`

<script>
$('#example-onDeselectAll').change(function() {
var value = $(this).val();
var i=0; 
var len = value.length;
var no_emp = $("#no_emp").val();
  for(i; i < len; i++){
var res = value[i] .split("_");
var tot_amount += res[1]*no_emp;
}
$("#no_emp3").val(tot_amount);
});
</scrit> 

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM