在javascript中下拉框值更改时如何获取文本框值 [英] How to get the textbox value when dropdown box value changed in javascript

查看:57
本文介绍了在javascript中下拉框值更改时如何获取文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在包含如下表的表

项目价格,数量总计

items  price  quantity  total

苹果100 2 2 200

apple    100     2         200

橙色200 2 2 600

orange  200    2           600

总计= 600.

项目字段是下拉列表,当下拉列表更改时,价格将更改,总价值和总计也将更改.我的问题是当再次选择苹果和橙子去苹果时,请更改我总计未更改的项目.

item fields are dropdown when drop down changes the price will be changed and total value and grandtotal also changed. My problem is when selecting apple and orange again go to apple change the item my grand total is not changing.

我的Javascript代码:

function totalprice(element, price) {
    var elementid = element.id;
    var expr = elementid.substring(elementid.indexOf(":") + 1, elementid.length);
    var quantity = document.getElementById("quantity:" + expr).value;
    var price = document.getElementById("price:" + expr).value;
    if (quantity > 0) {
        document.getElementById("total:" + expr).value = (parseInt(quantity)) * (parseInt(price));
        var grandtotal = document.getElementById("total:" + expr).value;
        var gtot = 0;
        var amount = 0;
        for (var i = 0; i <= expr; i++) {
            //document.getElementById("total").value="";
            gtot = document.getElementById("total:" + expr).value;
            amount = parseInt(gtot) + parseInt(amount);
        }
        document.getElementById("total").value = amount;
    }
    return true;
}

我知道错误只存在于for循环中,这很简单,但我不知道如何解决.

I know the mistake is in for loop only it is simple one but i dont know how to solve.

推荐答案

我使用表行长度来解决此问题,现在我的代码就像这样在我的for循环中使用该长度

I got the solution for this using table rows length and use that length to my for loop now my code is like

function totalprice(element,price)

{

var elementid=element.id;
var expr = elementid.substring(elementid.indexOf(":") + 1, elementid.length);
var quantity = document.getElementById("quantity:"+expr).value;

        var price = document.getElementById("price:" + expr).value; 
        if(quantity >0)
        {
        document.getElementById("total:"+ expr ).value= (parseInt(quantity))*(parseInt(price));           
        //var grandtotal =document.getElementById("total:"+expr).value;
        //var grandtotal = document.getElementsByClassName("total"+expr);

        var rowcount = document.getElementById('table').rows.length;

        var grandtotal = 0;
        var finalamount = 0;
            for(var i=1; i<rowcount; i++)
            {
                grandtotal=document.getElementById("total:"+i).value;
                finalamount = parseInt(grandtotal) + parseInt(finalamount);

            }

            document.getElementById("total").value=finalamount;
        }
      return true;

}

这篇关于在javascript中下拉框值更改时如何获取文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆