checkbox 多选代码

<script type="text/javascript"> 
var buildList = new Object;//创建对象

function build_edit(dom,buildid){//dom为触发该方法的checkbox对象(this),buildid为标识值
	if(dom.checked == true){
		buildList[buildid] = 1;//表示选中
	}else{
		buildList[buildid] = 0;//表示没选中
	}
	fillinbuildList();
}

function fillinbuildList(){//构建选中的checkbox值字符串
	var temp = "";
	for(key in buildList){
		if(buildList[key] == 1 && key != 0){
			temp += key + ",";
		}
	}
	temp = temp.substring(0,temp.length - 1);
	$("#buildList").val(temp);//"buildList"为jsp里一个文本框对象id,文本值为"xxxx,xxxx,xxxx"
}

function ini(){ //该方法为初始化buildList对象的值

	var ids = $("#buildList").val();//初始化时该值应自己从数据库取出
	var builds = ids.split(",");
	for(var i  = 0 ; i < builds.length ; i++){
		buildList[builds[i]] = 1;
	}
}


$("body").ready(ini);

</script>

你可能感兴趣的:(jsp)