繁体   English   中英

Knockout.js - 如果条件为真,则更改返回复选框值 null

[英]Knockout js - Change return checkbox value null if condition true

我是 Knockout.js 的新手,我有复选框和带条件的返回值。

如果条件为真,我想更改返回值 null,但我不知道如何:

isChecked: ko.computed(function () {
            return quote.paymentMethod() ? quote.paymentMethod().method : 'payu';
        }),

        isCheckedToShowTooltip: function () {
            $(".loading-mask").addClass("hide");

            if($(".payment-method").length == 0) {
                if ($(".multiple_payment_method").length == 0) {
                    $(".billing-address-list").before('<h4 class="multiple_payment_method"><span>Datos de envío</span></h4>');
                    $(".block.items-in-cart.active div.title").addClass("hide");
                    $(".block.items-in-cart").before('<h4 class="multiple_payment_method"><span>Resumen de tu compra</span></h4>');
                    $("#checkout-payment-method-load").before('<h4 class="multiple_payment_method"><span>Método de pago</span></h4>');
                }
                if ($(".multiple_payment_method").length == 3) {
                    this.isChecked() = null; //<----------------Here not work
                }
            }

            if(quote.paymentMethod()) {
                if($("#seccion-payu-below").length > 0) {
                    $("#seccion-payu-below").remove();
                }

                if($("#seccion-efecty-below").length > 0) {
                    $("#seccion-efecty-below").remove();
                }

                switch(quote.paymentMethod().method) {
                  case "payu":
                          $('#initial-payu-container').remove();
                          $('#checkout-payment-method-load').removeClass('container-initial');
                          $("#payment-methods-group-list" ).after("<div id='seccion-payu-below'>" + $("#seccion-payu").html() + "</div>");
                          $("#seccion-payu-below button").removeAttr("data-bind data-posicion data-origen");

                          if($("#seccion-payu button").hasClass("disabled")) {
                              $("#seccion-payu-below button").attr('disabled','disabled').addClass("disabled");
                          } else {
                              $("#seccion-payu-below button").removeAttr('disabled','disabled').removeClass("disabled");
                          }

                          $("#seccion-payu-below button").attr({ id: "button_payu_below", type: "button", onclick: "submitPaymentMethod('" + quote.paymentMethod().method + "');" });

                          if($('#payu').is(':checked')) {
                              $('#seccion-payu-below').show();
                          }
                    break;
                  case "efecty":
                          $('#initial-payu-container').remove();
                          $('#checkout-payment-method-load').removeClass('.container-initial');
                          $("#payment-methods-group-list" ).after("<div id='seccion-efecty-below'>" + $("#seccion-efecty").html() + "</div>");
                          $("#seccion-efecty-below button").removeAttr("data-bind data-posicion data-origen");

                          if($("#seccion-efecty button").hasClass("disabled")) {
                              $("#seccion-efecty-below button").attr('disabled','disabled').addClass("disabled");
                          } else {
                              $("#seccion-efecty-below button").removeAttr('disabled','disabled').removeClass("disabled");
                          }

                          $("#seccion-efecty-below button").attr({ id: "button_efecty_below", type: "button", onclick: "submitPaymentMethod('" + quote.paymentMethod().method + "');" });

                          if($('#efecty').is(':checked')) {
                              $('#seccion-efecty-below').show();
                          }
                    break;
                }
            }
        },

我试过提到的条件但没有工作

我知道读写属性,但我不知道这是否是我的情况

有任何想法吗? 谢谢!

感谢 epascarello 我编辑了条件,并解决了问题,为此添加了新的验证:

isChecked: ko.computed(function () {
            if (quote.paymentMethod()) {
                return quote.paymentMethod().method
            } else if (!rendererList._latestValue.some(item => item.type === 'efecty')) {
                return 'payu';
            } else {
                $('#checkout-payment-method-load').removeClass('container-initial');
                return null;
            }
        }),

问候!

暂无
暂无

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

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