87,921
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<div id="showSelected"></div>
<script>
(function(w,d){
var obj={},index= 0,arr=[],div=document.getElementById('showSelected');
function createCheckbox(value){
var input=d.createElement('input');
input.type='checkbox';
input.name='test';
input.value=value;
input.setAttribute('index',++index);
input.onclick=function(){
var _index=this.getAttribute('index');
if(this.checked){
obj[_index]=this.value;
}else{
delete obj[_index];
}
arr=[];
for(var i in obj){
if(typeof obj[i]=='string'){
arr.push(obj[i]);
}
}
div.innerHTML=arr.join();
}
return input;
}
function test(){
var arr=[1,2,3,4];
for(var i= 0,len=arr.length;i<len;i++){
d.body.appendChild(createCheckbox(arr[i]));
}
}
window.setTimeout(function(){
test();
},2000)
})(window,document)
</script>
</body>
</html>