动态添加下拉列表

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
<script type="text/javascript" language="javascript"> 
	//动态添加select 下拉框 (1)
	function changeSel(obj){
		var arr = new Array();
		arr[0]="测试一";
		arr[1]="测试二";
		arr[2]="测试三";
		var rowObj = obj.parentNode.parentNode;
		var rowNumber = rowObj.rowIndex;
		
		alert(rowNumber); 
		rowObj.getElementsByTagName("input")[1].value = "changed1";
		rowObj.getElementsByTagName("input")[2].value = "changed2";
		var cellObj = rowObj.cells[0];
		cellObj.innerHTML = "<select name = 'select' id = 'select' ></select>";
		for(var i = 0; i<arr.length; i++){
			var opt=new Option(arr[i],"1"); 
			rowObj.getElementsByTagName("select")[0].options[i]=opt;
		}
		
	}
	////动态添加select 下拉框 (2)
	function insertOption(obj)
	  {
	  var rowObj = obj.parentNode.parentNode;
	  var option1 = document.createElement('option');
	  option1.text='测试一';
	  var option2 = document.createElement('option');
	  option2.text='测试二';
      var cellObj = rowObj.cells[0];
	  cellObj.innerHTML = "<select name = 'select' id = 'select' ></select>";
	  var selObj = rowObj.getElementsByTagName("select")[0];
	  try
	    {
		  selObj.add(option1,null); // standards compliant
		  selObj.add(option2,null);
	    }
	  catch(ex)
	    {
		  selObj.add(option1); // IE only
		  selObj.add(option2);
	    }

	
	  selObj.options[selObj.selectedIndex].text="Melon"
	  }
</script>
</head>
<body>
	<table>
		<tbody id = "mainBody">
			<tr>	
				<td>
					<input type = "text" name = "text0" value = "text0" />
				</td>
				<td>
					<input type = "text" name = "text1" value = "text1" />
				</td>
				<td>
					<input type = "text" name = "text2" value = "text2" />
				</td> 	
				<td>
					<a href="#" onclick="changeSel(this)" >删除</a>
				</td> 
			</tr>
			<tr>	
				<td>
					<input type = "text" name = "text0" value = "text0" />
				</td>
				<td>
					<input type = "text" name = "text1" value = "text1" />
				</td>
				<td>
					<input type = "text" name = "text2" value = "text2" />
				</td> 	
				<td>
					<a href="#" onclick="changeSel(this)" >删除</a>
				</td> 
			</tr>
			<tr>	
				<td>
					<input type = "text" name = "text0" value = "text0" />
				</td>
				<td>
					<input type = "text" name = "text1" value = "text1" />
				</td>
				<td>
					<input type = "text" name = "text2" value = "text2" />
				</td> 
				<td>
					<a href="#" onclick="insertOption(this)" >删除</a>
				</td> 	
			</tr>
		</tbody>
	</table>			
</body>
</html>

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