每当选择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>

最佳答案

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

10-02 14:19