简体   繁体   English

限制单选按钮的点击次数并乘以数字

[英]Limit number of clicks on radio button and multiply by a number

As you can see in the jsfiddle, when the #CreditCard radio button is clicked, the Total is multiplied by 1.02 and the Total is updated. 正如您在jsfiddle中看到的,当单击#CreditCard单选按钮时,Total将乘以1.02并更新Total。

The problem is that the same button can be clicked multiple times and the number keeps multiplying. 问题是可以多次单击相同的按钮,并且数字会不断增加。

I want to limit the number of times a button can be clicked to 1, but also want to be able to switch between Debit card and Credit card. 我想限制点击按钮的次数为1,但也希望能够在借记卡和信用卡之间切换。 (so when the other button is clicked, the limit is reset to 0?) (所以当点击另一个按钮时,限制重置为0?)

Any help is much appreciated. 任何帮助深表感谢。

https://jsfiddle.net/n52fy9am/2/ https://jsfiddle.net/n52fy9am/2/

html HTML

Total: <span id="totalPrice">£154.67</span>
<br>

<form>

<input id="DebitCard" type="radio" name="payment" checked>
<label for="DebitCard">Debit Card</label>
<br>

<input id="CreditCard" type="radio" name="payment">
<label for="CreditCard">Credit Card (2% extra)</label>
<br>

</form>

js JS

// credit card 2% surcharge
$('#CreditCard').on("click", function() {
    var totalPrice = parseFloat($('#totalPrice').text().replace(/([^0-9\\.])/g, ""));
    var surcharge = (totalPrice * 1.02).toFixed(2);
    // Update total
    $('#totalPrice').html("£" + surcharge);
});

// remove 2%
$('#DebitCard').on("click", function() {
    var totalPrice = parseFloat($('#totalPrice').text().replace(/([^0-9\\.])/g, ""));
    var surcharge = (totalPrice * 0.98).toFixed(2);
    // Update total
    $('#totalPrice').html("£" + surcharge);
});

Just use a flag to do it. 只需用旗帜就可以了。 Change the flag when radio button clicked. 单击单选按钮时更改标志。 So that you can control whether to do the calculation or not. 这样您就可以控制是否进行计算。

Also, because you round it with toFixed(2) , the decimal part of the number will be messed up when you do the calculation. 此外,因为您使用toFixed(2)将其四舍五入,所以在进行计算时,数字的小数部分将会混乱。 So use a variable to store the initial value. 因此,使用变量来存储初始值。 Put it back when debitCard button is pressed. 按下debitCard按钮时将其放回debitCard处。

var isCreditCard = false;
var initialPrice = 154.67;
$('#totalPrice').html("£" + initialPrice.toString());

// credit card 2% surcharge
$('#CreditCard').on("click", function() {
    if (!isCreditCard) {
        var totalPrice = parseFloat($('#totalPrice').text().replace(/([^0-9\\.])/g, ""));
        var surcharge = (totalPrice * 1.02).toFixed(2);
        // Update total
        $('#totalPrice').html("£" + surcharge);

        isCreditCard = true;
    }
});

// remove 2%
$('#DebitCard').on("click", function() {
    if (isCreditCard) {
        // Update total
        $('#totalPrice').html("£" + initialPrice.toString());
        isCreditCard = false;
    }
});

I took your JS fiddle and turned into a stack snippet ;) 我把你的JS小提琴变成了一个堆栈片段;)

I modified the code and placed comments wherever necessary. 我修改了代码并在必要时放置了注释。 I store initial count values that get incremented on every click, if that number exceeds 0 then the function gets cancelled, this remains true until the other radio is checked which resets the previously clicked one. 我存储了每次点击增加的初始计数值,如果该数字超过0则该函数被取消,这将保持为真,直到检查另一个无线电,重置先前点击的那个。

 $(function() { // keep count of n times clicked var creditCardCount = 0; var debitCardCount = 0; // credit card 2% surcharge $('#CreditCard').on("change", function() { // if amount is greater or equal to 1 then abort the function if (creditCardCount > 0) { return false; } var totalPrice = parseFloat($('#totalPrice').text().replace(/([^0-9\\\\.])/g, "")); var surcharge = (totalPrice * 1.02).toFixed(2); // Update total $('#totalPrice').html("£" + surcharge); // increment credit card count creditCardCount++; // reset the other option to 0 debitCardCount = 0; }); // remove 2% // do the same here but for the opposite cards $('#DebitCard').on("change", function() { if (debitCardCount > 0) { return false; } var totalPrice = parseFloat($('#totalPrice').text().replace(/([^0-9\\\\.])/g, "")); var surcharge = (totalPrice * 0.98).toFixed(2); // Update total $('#totalPrice').html("£" + surcharge); debitCardCount++; creditCardCount = 0; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> Total: <span id="totalPrice">£154.67</span> <br> <form> <input id="DebitCard" type="radio" name="payment" checked> <label for="DebitCard">Debit Card</label> <br> <input id="CreditCard" type="radio" name="payment"> <label for="CreditCard">Credit Card (2% extra)</label> <br> </form> 

I suggest you simplify your code. 我建议你简化你的代码。 Check demo - Fiddle : 检查演示 - 小提琴

$(function() {
    var lastRadioClicked = 'DebitCard',
        canClick = true;

    $('[name="payment"]').on("click", function() {
        var surchargePercent;
        if (lastRadioClicked === this.id) {
            if (canClick) {
                if (this.id === 'DebitCard') {
                    // debit        
                    surchargePercent = 0.98;
                } else {
                    // credit    
                    surchargePercent = 1.02;
                }
                canClick = false;
                var totalPrice = parseFloat($('#totalPrice').text().replace(/([^0-9\\.])/g, ""));
                var surcharge = (totalPrice * surchargePercent).toFixed(2);
                // Update total
                $('#totalPrice').html("£" + surcharge);
            }
        } else {
            lastRadioClicked = this.id;
            canClick = true;
        }


    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM