通过选择复选框
问题描述:
function msg()
{
if (document.getElementById('c1').checked);
alert("suggest an update");
}
<input class="form-control" type="checkbox" id="c1" onclick="msg()" name="like"/>
答
下面将生成与一个文本输入元件的形式,很容易扩展,要包括许多其它元件。不能给任何jQuery的例子因为我不使用它。
<script type='text/javascript' charset='utf-8'>
function msg(e) {
var id='bob';
var el=e.target || e.srcElement;
if(el.checked){
var cont=document.getElementsByTagName('body')[0];
var form=addNode('form',{id:id},cont);
form.style.width='500px';
form.style.height='500px';
form.style.zIndex=10;
form.style.border='1px solid black';
addNode('input',{type:'text',name:'fred'},form);
}else{
if(document.getElementById(id)) document.getElementById(id).parentNode.removeChild(document.getElementById(id));
}
}
function addNode(t, a, p) {
var el = (typeof(t)=='undefined' || t==null) ? document.createElement('div') : document.createElement(t);
for(var x in a) if(a.hasOwnProperty(x) && x!=='innerHTML') el.setAttribute(x, a[ x ]);
if(a.hasOwnProperty('innerHTML')) el.innerHTML=a.innerHTML;
if(p!=null) typeof(p)=='object' ? p.appendChild(el) : document.getElementById(p).appendChild(el);
return el;
}
/*
The addNode function:
First argument is the type of element you wish to add.
Second argument is an object literal containing properties for the element you wish to set.
Third argument is the parent node to which you wish to append the new element/node.
*/
</script>
html
----
<input class="form-control" type="checkbox" id="c1" onclick="msg(event)" name="like"/>
+0
非常感谢你,但是当我取消选择复选框时,它应该关闭 –
+0
当然你可以添加它? – RamRaider
那就需要更多的解释去做。像其他网站的示例 – shanks
您可以使用JavaScript库,如jQuery – Sayed