如何使用javascript动态更改数量?

时间:2016-11-07 07:05:11

标签: javascript php mysql

<input type="checkbox" id="price"  value="<?php echo $test;?>"  onClick="valid(this);" name="test_name[]" />
<?php echo $data['test_name'];?>

这是我的输入,带有来自数据库的复选框。每个chechbox都有它们的价格。

<input onchange="multiply()" type="number" name="quantity" id="quantity"  placeholder="No. of Employees/Students" class="normal" required>  

这是用户输入数量的数量输入。

<script>    
    function valid(obj)
    {
        var frm=document.getElementById("frm");
        var qty=parseInt(document.getElementById("quantity").value);
        var price=parseInt(obj.value);
        var total=parseInt(document.getElementById("total").value);
        //var isFormSubmited = true;
        if(isNaN(total))
        {
            total=0;
        }           
        if(obj.checked==true)
        {
            total=total+(qty*price);
            document.getElementById("total").value=total;
        }
        if(obj.checked==false)
        {
            total=total-(qty*price);
            document.getElementById("total").value=total;
        }           
        document.getElementById("total").value=total
    }       

此脚本用于计算量和显示总金额。工作正常。

<script type="text/javascript">

    function multiply() 
    {   
        var frm=document.getElementById("frm");     
        var qty=parseInt(document.getElementById("quantity").value);        
        var total=0;
        for(var i=0;i<=frm.length;i++)
        {
            if(frm[i].type=="checkbox" && frm[i].name=="test_name[]")
            {
                if(frm[i].checked==true)
                {
                    var p=frm[i].value.split(",");
                    total=total+(parseInt(p[0])*qty);

                }
            }
        }
        document.getElementById("total").value=total;
    }

这是一个用于onchange数量的脚本总量应该动态改变。但它不起作用。请帮帮我。

0 个答案:

没有答案