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

时间:2018-11-08 07:43:11

标签: html5 css3 knockout-3.0

我已经编写了以下代码,以查看切换按钮的工作原理。但似乎数据绑定无法正常工作。

下面是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" />

但是它也不起作用。

点击事件将对此起作用。还是我们必须使用其他任何事件。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您要使用的绑定错误。 click绑定用于调用执行某些操作的函数。另一方面,可观测值和计算可观测值是用来保存值的。

您需要的是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>

相关问题