我有一个带有单选按钮和复选框的页面;该表格之前已由用户填写。我需要恢复表单以便他们编辑现有数据,这意味着我必须以编程方式检查复选框并检查单选按钮。
如果我有一个事件,我可以轻松地使用 srcElement 更改相应的控件,但在这种情况下没有事件。我一直无法找到使用 document.getElementById('XXX') 的方法。 ???以任何方式工作。
如果有帮助的话,请使用 Angular 4。
想法?
提前致谢,:-)
最佳答案
使用数据绑定(bind)。然后您可以设置更改的值,复选框和单选按钮将自动检查/选择。
这是一个例子:
<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";
}
}
关于javascript - 没有单选按钮或复选框的事件,如何以编程方式将其设置为 true 或 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45719991/