我将尝试回答您的替代问题。您可以创建一对元素 - 单选按钮输入和标签 - 并根据需要附加到 DOM。要创建“男性”和“女性”按钮,您可以这样做:
var button1 = new RadioButtonInputElement();
button1.name = "sex";
button1.value = "male";
button1.attributes['id'] = "sex_male";
var button1_label = new LabelElement();
button1_label.attributes['for'] = button1.id;
button1_label.text="Male";
var button2 = new RadioButtonInputElement();
button2.name = "sex";
button2.value = "female";
button2.attributes['id'] = "sex_female";
var button2_label = new LabelElement();
button2_label.attributes['for'] = button2.id;
button2_label.text="Female";
用于new RadioButtonInputElement()
创建无线电输入;用于new LabelElement()
创建随附的标签。
这段代码不是特别干燥;如果您要生成很多这些,您可能希望将其重构为一个函数。像这样的东西:
List createButtonWithLabel(name, value) {
var button = new RadioButtonInputElement();
button.name = name;
button.value = value;
button.attributes['id'] = "${name}_${value}";
var label = new LabelElement();
label.attributes['for'] = button.id;
label.text=value.toUpperCase();
return [button, label];
}
然后像这样使用它:
var form = new FormElement();
form.nodes.addAll(createButtonWithLabel('sex', 'male'));
form.nodes.addAll(createButtonWithLabel('sex', 'female'));
query('body').nodes.add(form);
这应该会给你一个带有 2 个单选按钮的表单。