如果没有单选按钮或复选框的事件,我如何以编程方式将其设置为 true 或 false



我有一个带有单选按钮和复选框的页面;该表单以前已由用户填写。 我需要恢复表单以便他们编辑现有数据,这意味着我必须以编程方式选中复选框并选中单选按钮。

如果我有一个事件,我可以轻松地使用 srcElement 更改相应的控件,但在这种情况下没有事件。 我无法找到使用document.getElementById('XXX'(的方法。??? 以任何方式工作。

如果有帮助,请使用 Angular 4。

思潮?

提前感谢,:-(

使用数据绑定。然后,您可以设置更改的值,复选框和单选按钮将自动选中/选择。

下面是一个示例:

<label>
<input id="sendCatalogId"
type="checkbox"
[(ngModel)]="sendCatalog"
name="sendCatalog" >
Send me your catalog
</label>

还有一个单选按钮:

<label class="radio-inline">
<input type="radio" id="addressType1Id" value="home"
[(ngModel)]="addressType"
name="addressType">Home
</label>
<label class="radio-inline">
<input type="radio" id="addressType1Id" value="work"
[(ngModel)]="addressType"
name="addressType">Work
</label>
<label class="radio-inline">
<input type="radio" id="addressType1Id" value="other"
[(ngModel)]="addressType"
name="addressType">Other
</label>

这在组件类中:

...
export class CustomerComponent  {
sendCatalog = false;
addressType = "home";
constructor() {
sendCatalog = true;
addressType = "work";
}
}

最新更新