admin管理员组文章数量:1418593
I have a html table built from a DB. It has a "Edit" button that will open up a form.
One of those input types will be a radio button I want to generate with On/Off selection:
<input type="radio" name="camera_status" value="On" /> On<br />
<input type="radio" name="camera_status" value="Off" /> Off
I have 3 problems:
I don't know how to create multiple radio options. The code below only creates one.
How do I make the value I passed in "camerastatus" the default checked value?
I need to have multiple labels. The form will be "Camera status" then labels for On/Off.
Code:
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);
}
I have a html table built from a DB. It has a "Edit" button that will open up a form.
One of those input types will be a radio button I want to generate with On/Off selection:
<input type="radio" name="camera_status" value="On" /> On<br />
<input type="radio" name="camera_status" value="Off" /> Off
I have 3 problems:
I don't know how to create multiple radio options. The code below only creates one.
How do I make the value I passed in "camerastatus" the default checked value?
I need to have multiple labels. The form will be "Camera status" then labels for On/Off.
Code:
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);
}
Share
Improve this question
edited Dec 7, 2011 at 3:04
Jared Farrish
49.3k17 gold badges99 silver badges107 bronze badges
asked Dec 7, 2011 at 2:59
TomTom
2,63411 gold badges61 silver badges96 bronze badges
1
- "I don't know how to create multiple radio options. The code below only creates one." - So take the bit that creates one and repeat it. Also, get rid of the try/catch and just do it the way you've got in the catch (along with the two lines after the end of the catch block). But wouldn't a checkbox be better suited to an on/off field? – nnnnnn Commented Dec 7, 2011 at 3:14
2 Answers
Reset to default 2To make a radio button the default, add the checked attribute.
<input type="radio" name="..." checked />
Wouldn't it be better to use a checkbox for something that can be on or off?
<input type="checkbox" ... />
"Male" value is checked:
<div><input type="radio" name="Gender" value="Male" checked="checked" /> Male</div>
<div><input type="radio" name="Gender" value="Female" checked="" /> Female</div>
To do this in JavaScript you could just put a block of code where you needed the radio buttons, and use document.write with a for loop for each radio button you need. (You'll also want to change the value every time too, of course.)
-Neil
本文标签: htmlcreate radio button with javascriptStack Overflow
版权声明:本文标题:html - create radio button with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745287671a2651597.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论