简体   繁体   English

如何禁用此保存/提交按钮?

[英]How to Disable this save/submit button?

I am wondering what is the best method to disable this save button after one click?我想知道单击后禁用此保存按钮的最佳方法是什么?

Here is the code:这是代码:

<button data-bind="enable: !$root.isSaving(), click: $root.addNewCard.bind($data, $root)" class="primary button" role="button" data-size="sm">
                                Save
                                </button>

Above is the.cshtml code以上是.cshtml代码

Below is the javascript code:下面是javascript代码:

BillingInformation.prototype.addNewCard = function (parent, creditCard) {
parent.isSaving(true);
parent.isSettingDefault(true);

creditCard.cardNumber(creditCard.cardNumber().replace(/-|\s/g, ''));

let $form = $('#addNewCardForm' + creditCard.walletItemId()),
    cardNumber = creditCard.cardNumber();

parseDynamicFormElements($form);

if ($form.valid()) {
    verifyAddress($form, function () {
        authentication.checkReAuthenticatedThenLaunchLoginOrExecuteTask(() => {
            creditCard
                .save()
                .then(response => {
                    if (response.status === HTTP_STATUS_OK) {
                        creditCard.walletItemId(response.content);
                        parent.walletItems.push(creditCard);
                        creditCard.lastFourDigits(
                            creditCard.cardNumber() ? creditCard.cardNumber().substr(-4) : ''
                        );
                        creditCard.obfuscatedCardNumber(cardNumber.replace(/.(?=.{4})/g, '*'));
                        parent.newCardInstance(new CreditCard({paymentType: 'creditCards'}));
                        checkForRedirect();
                    } else {
                        let container = document.createElement('div');
                        container.append(
                            'We were unable to save your payment information. Please try again or give us a call at 1-800-461-8898'
                        );
                        smartPak.ui.modal(container, {
                            title: 'Unable to Save Payment Method',
                            buttons: {
                                Close: function (modal) {
                                    modal.close();
                                }
                            }
                        });
                    }
                })
                .then(() => {
                    parent.isSaving(false);
                });
        });
    });
    parent.isSaving(false);
} else {
    parent.isSaving(false);
    parent.isSettingDefault(false);
}

}; };

What is the best method to prevent this from being clicked more than once after submission?防止在提交后被多次点击的最佳方法是什么? currently if clicked multiple times it will duplicate the cc.目前,如果点击多次,它将复制 cc。

Thank you!谢谢!

Maybe the the enable property should bind to a variable, not a function like you do in也许 enable 属性应该绑定到一个变量,而不是像你在

enable: !$root.isSaving()

did you try switching click: $root.addNewCard.bind($data, $root) to click: $root.addNewCard.bind($root, $data)?您是否尝试将点击:$root.addNewCard.bind($data, $root) 切换为点击:$root.addNewCard.bind($root, $data)? according to your declaration BillingInformation.prototype.addNewCard = function (parent, creditCard) creditCard = $data from the form and parent = $root?根据您的声明 BillingInformation.prototype.addNewCard = function (parent, creditCard) creditCard = $data from the form and parent = $root?

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

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