| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 890 人关注过本帖
标题:checkboxlist的改进加优化版,增加添加删除功能
只看楼主 加入收藏
linuxpluto
Rank: 4
等 级:贵宾
威 望:13
帖 子:889
专家分:23
注 册:2005-8-14
收藏
 问题点数:0 回复次数:1 
checkboxlist的改进加优化版,增加添加删除功能

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>CheckBoxList的列表框</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
body,td{font-size:12px;color:#000000;}
.checkbox{width:15px;height:15px;}
.cked{
margin:1px;padding:2px;width:100%;display:block;background-color:highlight;color:highlighttext;
}
.nock{
margin:1px;padding:2px;width:100%;display:block;
}
</style>
</head>

<script LANGUAGE="JavaScript">
<!--
function checkItem (name,str) {
this.htmlcode='';
this.option=new Array();

function HtmlEncode(text){
return text.replace(/&/g, '&amp').replace(/\"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

this.init=function () {
if (str=="")
return
var arr=str.split('^');
for(var i=0;i<arr.length;i++)
{
var t,v,temp='';
var thisarr=arr[i].split('@=');
if (thisarr.length==2)
{
t=HtmlEncode(thisarr[0]);
v=HtmlEncode(thisarr[1]);
}
else
{
t=v=HtmlEncode(arr[i]);
}
temp+="<label class=\"nock\" for=\"i_"+name+"_"+i+"\" id=\"l_"+name+"_"+i+"\">";
temp+="<input class=\"checkbox\" onpropertychange=\"document.getElementById('l_"+name+"_"+i+"').className=(document.getElementById('i_"+name+"_"+i+"').checked)?'cked':'nock';\" onclick=\"document.getElementById('l_"+name+"_"+i+"').className=(document.getElementById('i_"+name+"_"+i+"').checked)?'cked':'nock';\" type=\"checkbox\" name=\""+name+"\" id=\"i_"+name+"_"+i+"\" value=\""+v+"\" \/> ";
temp+=t+"</label>";
this.option.push(temp);
}
for (i=0;i<this.option.length;i++)
if (this.option[i])
this.htmlcode+=this.option[i]
}

this.getV=function (o) {
if (!o)
return
var allvalue=new Array();
var itemList=new Array();
for(var i=0;i<o.length;i++)
{
if(o[i].checked){
allvalue.push(o[i].value);
itemList.push(o[i].id);
}
}
itemList.push(allvalue)
return itemList;//返回数组,最后一个值是要提交的数据的数组,前面的是每个值的id值
}

this.removeItem=function (o) {
if (!o)
return//值不存在就返回
//var delOption=String(o).split(",");
var tempA=new Array();
for (i=0;i<this.option.length;i++)
{
for (m=0;m<o.length;m++)
{
if (this.option[i].match(o[m]))
{
this.option[i]="";
break;
}
else if (m!=(o.length-1))
{
continue;
}
}
}
this.htmlcode="";
for (i=0;i<this.option.length;i++)
if (this.option[i])
this.htmlcode+=this.option[i]
}

this.addItem=function (addstr) {
if (!addstr)
return
for (var i=0;this.option[i];i++);//循环遍历找到option中值为空的i
if (!this.option[i])
{
var thisarr=addstr.split("@=");
if (thisarr.length)
{
var t,v,temp;
if (thisarr.length==2)
{
t=HtmlEncode(thisarr[0]);
v=HtmlEncode(thisarr[1]);
}
else
{
t=v=HtmlEncode(addstr);
}
temp ="<label class=\"nock\" for=\"i_"+name+"_"+i+"\" id=\"l_"+name+"_"+i+"\">";
temp+="<input class=\"checkbox\" onpropertychange=\"document.getElementById('l_"+name+"_"+i+"').className=(document.getElementById('i_"+name+"_"+i+"').checked)?'cked':'nock';\" onclick=\"document.getElementById('l_"+name+"_"+i+"').className=(document.getElementById('i_"+name+"_"+i+"').checked)?'cked':'nock';\" type=\"checkbox\" name=\""+name+"\" id=\"i_"+name+"_"+i+"\" value=\""+v+"\" \/> ";
temp+=t+"</label>";
this.option[i]=temp;
this.htmlcode="";
for (i=0;i<this.option.length;i++)
if (this.option[i])
this.htmlcode+=this.option[i]
}
}
}
this.init();
}
//-->
</script>

<body>
<form method="post" name="myform" action="?">
<table border="1" width="200">
<tr>
<td><div id=checkboxitem style="width:180px;height:150px;overflow:auto;border: 2px inset #FFFFFF;">
<script LANGUAGE="JavaScript">
<!--

var aa=new checkItem("city","北京^山东^香港^澳门^台湾")

function hide () {
checkboxitem.innerHTML=''
}

function show () {
checkboxitem.innerHTML=aa.htmlcode
}

function del() {
aa.removeItem(aa.getV(document.myform.city))
checkboxitem.innerHTML=aa.htmlcode
}

function add () {
aa.addItem("aaa")
checkboxitem.innerHTML=aa.htmlcode
}
//-->
</script>
</div></td>
</tr>
<tr>
<td><button onclick="hide()">隐藏</button>
<button onclick="show()">显示</button>
<button onclick="del()">remove</button>
<button onclick="add();">addItem</button>
<button onclick="alert(aa.getV(document.myform.city).join('-'));">get值</button> </td>
</tr>
</table>
</form>
</body>
</html>

[此贴子已经被作者于2006-2-13 7:13:46编辑过]

搜索更多相关主题的帖子: checkboxlist 改进 删除 
2006-02-13 07:13
linuxpluto
Rank: 4
等 级:贵宾
威 望:13
帖 子:889
专家分:23
注 册:2005-8-14
收藏
得分:0 

运行有出问题了


吃的比猪还差,干的比驴还累,起的比鸡还早,睡得比小姐还晚,挣得比民工还少,看起来比谁都好——苦命的人.人生短短几十年,不要给自己留下了什么遗憾,想笑就笑,想哭就哭,该爱的时候就去爱,无谓压抑自己
2006-02-13 07:14
快速回复:checkboxlist的改进加优化版,增加添加删除功能
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017684 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved