我有一个由DB构建的html表。它有一个“编辑”按钮,可以打开一个表单。
其中一种输入类型是我想通过开/关选择生成的单选按钮:
<input type="radio" name="camera_status" value="On" /> On<br />
<input type="radio" name="camera_status" value="Off" /> Off
我有3个问题:
我不知道如何创建多个无线电选项。以下代码仅创建一个。
如何将我在“camerastatus”中传递的值设为默认选中值?
我需要有多个标签。表格将为“相机状态”,然后为开/关标签。
代码:
function edit2(to, cameraname, cameraquality, camerastatus, emailnotice, camerahash)
{
var mydiv = document.getElementById("editform");
var myForm = document.createElement("form");
myForm.method = "post";
myForm.action = to;
//camera_status
label = document.createElement("label");
label.for = "text";
label.innerHTML="<br>Camera status: <br>";
myForm.appendChild(label);
var myRadioElement;
try{
myRadioElement = document.createElement('<input type="radio" name="camera_status" />On');
}catch(err){
myRadioElement = document.createElement('input');
}
myRadioElement.type = "radio";
myRadioElement.name = "camera_status";
myForm.appendChild(myRadioElement);
mydiv.appendChild(myForm);
}
答案 0 :(得分:2)
要将单选按钮设为默认值,请添加选中的属性。
<input type="radio" name="..." checked />
对于可以打开或关闭的内容,使用复选框会不会更好?
<input type="checkbox" ... />
答案 1 :(得分:0)
检查“男性”值:
<div><input type="radio" name="Gender" value="Male" checked="checked" /> Male</div>
<div><input type="radio" name="Gender" value="Female" checked="" /> Female</div>
要在JavaScript中执行此操作,您只需将代码块放在需要单选按钮的位置,然后对所需的每个单选按钮使用带有for循环的document.write。 (当然,你也希望每次都改变价值。)
-Neil