日期:2014-05-16  浏览次数:20378 次

js实现的复选框选中时的批量操作

1.

var checkedList = [];	// save for all checked  box
var tmpHotel = new Hotel();
tmpHotel.setHotelId("${item.key}");
checkedList[tmpHotel.getHotelId()] = [];  //将每个hotelId对应的行也初始化为一个数组

<td class="htbl_td1_'+tr_border+'">
	<input onclick="modifyCheckedList(\''+ hotels[i].getHotelId() +'\', this, '+ hotels[i].getRooms(rTypeCnt).getInternalId(j) +');" type="checkbox" />
</td>;
?
	//点击每行(room行)的复选框时,将该行对应的internalId放入二维数组checkedList[hotelId][checkedList[hotelId].length] 项中
	function modifyCheckedList(hotelId, chk, internalId) {
		
		if ( chk.checked == true) checkedList[hotelId][checkedList[hotelId].length] = internalId;
		
		for (var i=0; i<checkedList[hotelId].length; i++) {
			if (checkedList[hotelId][i] == internalId) {
       if (chk.checked == true) {
					// Do nothing here
				} else {
					checkedList[hotelId][i] = 0;			
 // remove the internalid form checkedList array
					setDeleteLinkVisible(hotelId);
					setBatchUpdateVisible(hotelId);
					return;
				}
			}
			if (checkedList[hotelId][i] == 0) {
					if (chk.checked == true) { checkedList[hotelId][i] = internalId; }
					setDeleteLinkVisible(hotelId);
					setBatchUpdateVisible(hotelId);
					return;
			}
		}
		setDeleteLinkVisible(hotelId);
		setBatchUpdateVisible(hotelId);
	}
?
	//显示批量操作按钮
	function setBatchUpdateVisible(hotelId) {
		var noTick = true;
		for (var i=0; i<checkedList[hotelId].length; i++) {
			if (parseInt(checkedList[hotelId][i]) != 0) {
				noTick = false;
				break;
			}
		}
		if (noTick == true) {
			$("batchUpdateLink" + hotelId).style.display = "none";
		} else {
			$("batchUpdateLink" + hotelId).style.display = "";
		}	
	}
?
	//点击批量操作按钮
	//从checkedList[hotelId][..]中读取选中的复选框项,将每行对应的internalId放入一个String类型的变量updateList中,格式如 95599,96887,77845
	//在Java类中分割即可获得选中的复选框项,如:String[] tariffIdArr = request.getParameter("updateList").split(",");
	function doBatchUpdate(hotelId) {	
		if (checkedList[hotelId].length == 0) return;
		var noTick = true;
		for (var i=0; i<checkedList[hotelId].length; i++) {
			if (parseInt(checkedList[hotelId][i]) != 0) { noTick = false; break; }
		}
		if (noTick == true) { return; }
		if(!confirm("Do you confirm update?")) return;
		
		var updateList = "";
		for(var i=0; i<checkedList[hotelId].length-1; i++) {
			updateList += checkedList[hotelId][i] + ",";
		}
		updateList += checkedList[hotelId][checkedList[hotelId].length-1];
		var url = "<%=basePath%>tariffBatchUpdate.do?hotelId="+hotelId+"&timeStamp=" + new Date().getTime();
		var formBatchUpdate = document.createElement("FORM"); //创建一个form
		formBatchUpdate.setAttribute("action", url);
		formBatchUpdate.setAttribute("method","post");
		var updateListInput = document.createElement("input"); //创建一个input表单
		updateListInput.setAttribute("name","updateList");
		updateListInput.setAttribute("type","hidden");
		updateListInput.setAttribute("value",updateList);
		formBatchUpdate.appendChild(updateListInput);   //将表单加入Form中
		document.body.appendChild(formBatchUpdate);     //将Form加入body中
		formBatchUpdate.submit();
	}

?11