CSS拨动开关不会更改复选框的值/状态 [英] css Toggle switch does not change checkbox value/state

查看:137
本文介绍了CSS拨动开关不会更改复选框的值/状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个切换开关. 我使用的模板来自:

I am trying to create an toggle a switch. the tempate I used is from:

https://www.w3schools.com/howto/howto_css_switch.asp

一切正常,但(隐藏)复选框的选中或值不变.我需要这个值进行我的angular(4)绑定

every things works fine but the checked or value of the (hidden) checkbox does not change. I need this value for my angular(4) binding

您如何做到这一点?

推荐答案

您可以使用绑定来实现.

you can do this using binding.

<input type="checkbox" [checked]="checkbx"
(change)="checkbx = !checkbx" formControlName="name">

In Ts file:
private checkbx: boolean = true;

constructor(private fb: FormBuilder){
    this.heroForm = this.fb.group({
    name: [this.checkbx, Validators.required ],
    });
}

addvalue(checkbx: boolean){
  console.log(this.heroForm.value)
}

选中此实时链接

这篇关于CSS拨动开关不会更改复选框的值/状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆