The example in this article introduces how to dynamically add a checkbox using JavaScript:
In actual applications, it may be necessary to dynamically add a checkbox. Here is a brief introduction on how to achieve this effect.
It is very easy to simply create a checkbox, the code is as follows:
var oCheckbox=document.createElement("input"); oCheckbox.setAttribute("type","checkbox"); oCheckbox.setAttribute("id","mayi");
But this It just creates a checkbox object, but it often cannot meet actual needs, because in actual applications, there is usually explanatory text in front of or behind the checkbox. Here is an introduction to how to achieve this effect:
The method is to create a checkbox object, then create a text node, and then add it to p.
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>添加checkbox复选框</title> <script type="text/javascript"> var oCheckbox=document.createElement("input"); var myText=document.createTextNode("蚂蚁部落"); oCheckbox.setAttribute("type","checkbox"); oCheckbox.setAttribute("id","mayi"); window.onload=function(){ var mydiv=document.getElementById("mydiv"); mydiv.appendChild(oCheckbox); mydiv.appendChild(myText); } </script> </head> <body> <div id="mydiv"></div> </body> </html>
The above is the method of dynamically adding a checkbox in javascript_javascript skills. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!