如何使用敲除绑定切换按钮

How to bind a toggle button using knock out

我已经编写了以下代码来查看切换按钮的工作情况。但似乎数据绑定不起作用。

下面是HTML代码

  <div>
        <label class="switch">
            <input type="checkbox" id="toggle123" data-bind="click:isToggleTrueOrFalse" />
            <span class="slider round"></span>
        </label>
    </div>

样式如下

<style>

         .switch {
            position: relative;
            display: inline-block;
            width: 60px;
            height: 34px;
        }

        .switch input {
            opacity: 0;
            width: 0;
            height: 0;
        }

        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            -webkit-transition: .4s;
            transition: .4s;
        }

            .slider:before {
                position: absolute;
                content: "";
                height: 26px;
                width: 26px;
                left: 4px;
                bottom: 4px;
                background-color: white;
                -webkit-transition: .4s;
                transition: .4s;
            }

        input:checked + .slider {
            background-color: #2196F3;
        }

        input:focus + .slider {
            box-shadow: 0 0 1px #2196F3;
        }

        input:checked + .slider:before {
            -webkit-transform: translateX(26px);
            -ms-transform: translateX(26px);
            transform: translateX(26px);
        }

        /* Rounded sliders */
        .slider.round {
            border-radius: 34px;
        }

            .slider.round:before {
                border-radius: 50%;
            }


    </style>

下面是脚本

<script>
    self.isToggleTrueOrFalse = ko.computed(function () {
        if (document.getElementById('toggle123').checked) {
            // return true;
            console.log("true");
        }
        else {
            console.log("false");
        }
    }, self);
</script>

我的要求是在单击切换按钮时我必须在控制台中获得 true 或 false。目前只有 false 出现在控制台中。

我也试过使用

<script>
        this.toggleAssociation1=function () {
            var checkBox = document.getElementById("toggle123");
            if (checkBox.checked == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    </script>

<input type="checkbox" id="toggle123" data-bind="click:toggleAssociation1" />

但也没有用。

将为此点击事件工作。或者我们必须使用任何其他事件。

谁能帮我解决这个问题。

您为要实现的目标使用了错误的绑定。 click 绑定用于调用执行某些操作的函数。另一方面,Observables 和 Computed Observables 旨在保存值。

您需要的是 checked 绑定 (https://knockoutjs.com/documentation/checked-binding.html)。一旦你使用它,你就不需要下面的代码了——

var checkBox = document.getElementById("toggle123");
  if (checkBox.checked == true){
     return true;
  }
  else{
     return false;
  }

这里是checked绑定的使用方法:

function viewmodel(){
  var self = this;
  self.isToggleTrueOrFalse = ko.observable();
  
  //to check the value whenever it updates, we can subscribe to the observable like this:
  self.isToggleTrueOrFalse.subscribe(function(latestValue){
    console.log(latestValue);
  });
}

ko.applyBindings(viewmodel());
.switch {
 position: relative;
 display: inline-block;
 width: 60px;
 height: 34px;
}

.switch input {
 opacity: 0;
 width: 0;
 height: 0;
}

.slider {
 position: absolute;
 cursor: pointer;
 top: 0;
 left: 0;
 right: 0;
 bottom: 0;
 background-color: #ccc;
 -webkit-transition: .4s;
 transition: .4s;
}

.slider:before {
 position: absolute;
 content: "";
 height: 26px;
 width: 26px;
 left: 4px;
 bottom: 4px;
 background-color: white;
 -webkit-transition: .4s;
 transition: .4s;
}

input:checked+.slider {
 background-color: #2196F3;
}

input:focus+.slider {
 box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
 -webkit-transform: translateX(26px);
 -ms-transform: translateX(26px);
 transform: translateX(26px);
}


/* Rounded sliders */

.slider.round {
 border-radius: 34px;
}

.slider.round:before {
 border-radius: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<div>
  <label class="switch">
    <input type="checkbox" id="toggle123" data-bind="checked:isToggleTrueOrFalse" />
    <span class="slider round"></span>
  </label>
</div>