1

I would like to add a $5.00 charge whenever the txtBwayEDUGift checkbox is selected. 我目前拥有的 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>
4

3 回答 3

0

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

于 2013-01-04T22:49:08.213 回答
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# 页面的引用之外:

if (txtBwayEDUGift.Checked)
        {
            addDonations(5.00, 93);
        }
于 2013-01-08T15:35:12.603 回答
-1

你可以使用 jQuery :)

$(txtBwayEDUGift).change(calculateTotal(5, "S"));
于 2013-01-04T19:18:50.730 回答