操作select控件,增、删、改

<html>
<head>
<script language="javascript">
function addUCR(formObj){
	newUCR = formObj.ucr.value;
	if(newUCR=='') return;
	objUCRList = formObj.ucrList;
	if(!isUCRExists(formObj,newUCR)){
		newOption = new Option(newUCR,newUCR);
		objUCRList.options[objUCRList.options.length]=newOption;
		formObj.ucr.value='';
	}

}

function updateUCR(formObj){
	newUCR = formObj.ucr.value;
	if(newUCR=='') return;
	objUCRList = formObj.ucrList;
	if(objUCRList==null) return;
	sIndex = objUCRList.selectedIndex;
	if(sIndex==-1) return;
	if(!isUCRExists(formObj,newUCR)){
		objUCRList.options[sIndex].value = newUCR;
		objUCRList.options[sIndex].text = newUCR;
		objUCRList.selectedIndex = -1;
	}

}

function deleteUCR(formObj){
	objUCRList = formObj.ucrList;
	if(objUCRList==null) return;
	for(i=objUCRList.options.length-1;i>=0;i--){
		if(objUCRList.options[i].selected){
			objUCRList.remove(i);
		}
	}
	for(i=0;i<sIndex.length;i++){
	objUCRList.options[sIndex[i]] = null;
	}
}

function isUCRExists(formObj,ucr){
	objUCRList = formObj.ucrList;
	if(objUCRList!=null){
		for(i=0;i<objUCRList.options.length;i++){
			if(objUCRList.options[i].value==ucr){
			return true;
			}
		}
	}
	return false;
}

function setUCR(formObj){
	objUCR = formObj.ucr;
	objUCRList = formObj.ucrList;
	if(objUCR!=null && objUCRList!=null){
		sIndex = objUCRList.options.selectedIndex;
		objUCR.value = objUCRList.options[sIndex].value;
	}
}

function selectAllUCR(formObj){
	if(formObj == null) return false;
	objUCRList = formObj.ucrList;
	if(objUCRList==null) return false;

	for(i=0;i<objUCRList.options.length;i++){
		objUCRList.options[i].selected=true;
	}
	return true;
}

</script>
</head>
<body>
<form name="abc">
	<input type="text" name="ucr" maxlength="35" size="25">
	<input type="button" value="Add" onClick="addUCR(this.form)">
	<input type="button" value="Update" onClick="updateUCR(this.form)"><Br>
	<select multiple="multiple" name="ucrList" size="7" onChange="setUCR(this.form)">
	</select>
	<input type="button" value="Delete" onClick="deleteUCR(this.form)">
	<Br><BR><BR>
	<input type="button" class="btnSubmit" name="Save" value="Save" onclick="selectAllUCR(this.form)">
</form>
</body>
</html>

 

你可能感兴趣的:(JavaScript,html)