选中复选框时将价格添加到总计

时间:2013-01-04 19:12:31

标签: javascript asp.net

每当选中txtBwayEDUGift复选框时,我想添加$ 5.00的费用。我目前拥有的javascript代码是在取消选中复选框时减少金额,但在选中时不应用费用。如果需要,我可以提供额外的代码。

这是我的aspx页面中的输入类型:

<input type="checkbox" name="txtBwayEDUGift" id="txtBwayEDUGift" onchange="checkboxAdd(this);" checked="checked" />

这是我的javascript:

{
    var divPrevAmt;
    if (type == 0)
    {
        divPrevAmt = document.getElementById("divBwayGiftPrevAmt");
    }
    else if (type == 1)
    {
        divPrevAmt = document.getElementById("divBwayEDUGiftPrevPmt");
    }
    var txtAmt = document.getElementById(obj);
    var amt = txtAmt.value;
    amt = amt.toString().replace("$","");
    amt = amt.replace(",","");
    var prevAmt = divPrevAmt.innerHTML;
    try
    {
        amt = amt * 1;
    }
    catch(err)
    {
        txtAmt.value = "";
        return;
    }
    if (amt >= 0) //get the previous amount if any
    {
        if (type == 0)
        {
           if (prevAmt.toString().length > 0)
           {
                prevAmt = prevAmt * 1;
           }
           else
           {
                prevAmt = 0;
           }
        }
        else if (type == 1)
        {
           if (prevAmt.toString().length > 0)
           {
                prevAmt = prevAmt * 1;
           }
           else
           {
                prevAmt = 0;
           }
        }
    //now update the master total
    var total = document.getElementById("txtTotal");
    var dTotal = total.value.toString().replace("$","");
    dTotal = dTotal.replace(",","");
    dTotal = dTotal * 1;
    var newTotal = dTotal - prevAmt;
    newTotal = newTotal + amt;
    divPrevAmt.innerHTML = amt.toString();
    newTotal = addCommas(newTotal);
    amt = addCommas(amt);
    txtAmt.value = "$" + amt;
    total.value = "$" + newTotal;
   }
   else
   {
        txtAmt.value = "";
        return;
   }
}
function disable()
{
    var txtTotal = document.getElementById("txtTotal");
    var txt = txtTotal.value;
    txtTotal.value = txt;
    var BwayGift = document.getElementById("txtBwayGift");
    BwayGift.focus();
}
function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    var newTotal = x1 + x2;
    if (newTotal.toString().indexOf(".") != -1)
    {
        newTotal = newTotal.substring(0,newTotal.indexOf(".") + 3);
    }
    return newTotal;
}
function checkChanged()
{
    var cb = document.getElementById("cbOperaGala");
    if (cb.checked == true)
    {
        var tableRow = document.getElementById("trCheckbox");
        tableRow.style.backgroundImage = "url('images/otTableRowSelect.jpg')";
    }
    else if (cb.checked == false)
    {
        var tableRow = document.getElementById("trCheckbox");
        tableRow.style.backgroundImage = "";
    }
}
function alertIf()
{
    var i = 0;
    for (i=5;i<=10;i++)
    {
        try{
        var subtotal2 = document.getElementById("txtSubTotal" + i);
        var dSubtotal2 = subtotal2.value;
        dSubtotal2 = dSubtotal2.replace("$","");
        dSubtotal2 = dSubtotal2 * 1;}
        catch (Error){dSubtotal2 = 0}
        if (dSubtotal2 > 0)
        {
            alert("You have selected the I want it all package, \n however you have also selected individual tickets to the same events. \n If you meant to do this, please disregard this message.");
            break;
        }
    }
}

function disableEnterKey(e)
{
 var key;      
 if(window.event)
      key = window.event.keyCode; //IE
 else
      key = e.which; //firefox      

return (key != 13);
}

    //Add $5.00 donation to cart

    function checkboxAdd(ctl) {
    if (ctl.checked == true) {
    //            alert("adding $5");
        calculateTotal(5, "A");
    } else {
    //            alert("deducting $5");
        calculateTotal( 5, "S");
    }
} 

</script>

3 个答案:

答案 0 :(得分:0)

我不了解该场景的背景。当用户单击该复选框时,您是否向服务器发出HTTP请求?或者这是一个简单的表单POST页面?什么是calculateTotal()在做什么?

答案 1 :(得分:0)

我明白了。所以我的功能很好。

//Add $5.00 donation to cart
function checkboxAdd(txtBwayEDUGift) {
    if (txtBwayEDUGift.checked == true) {
//            alert("adding $5");
        calculateTotal(5, "A");
    } else {
//            alert("deducting $5");
        calculateTotal(5, "S");
    }
}

但我需要添加一个加载函数:

function load() {
    calculateTotal(5, "A");
}

<body onload="load()">

除了添加对c#page的引用外:

if (txtBwayEDUGift.Checked)
        {
            addDonations(5.00, 93);
        }

答案 2 :(得分:-1)

你可以使用jQuery:)

$(txtBwayEDUGift).change(calculateTotal(5, "S"));