JavaScriptの例—行の増加、削除、全選択、逆選択を実現する.


今日はたまたま前の音楽サイトで音楽サイトの中で全選択と反対選択の操作を提供しています.いいと思います.加えて最近のプロジェクトに似ているところがあります.
   説明:addItem()は実際に車をノックしてから自動的に一行を追加します.このようにユーザー体験がいいです.
   実現機能:行の増加、削除、全選択、逆選択、金額の計算、四捨五入などを実現します.
   問題:テンプレートを使って新しい行のaddを追加すると、最初の行のどのデータも次の新しく追加された行に持ち込まれます.
   解決方法:テンプレートaddsを使わずに新しい行を追加すればこの問題を解決できます.
customer.js:

/**
 * @author fuhao
 * @param {Object} tbodyid
 *    Html          table  
 * table     tbody
 * tbody     tr
 * tr     td
 * td     checkbox input
 */
//  table     ,        
//         :                  
//     :adds(tbodyid)          
function add(tbodyid){
	if(tbodyid==null){
		alert("           ");
		return ;
	}
	//   tbody,     prototype.js   $(tbodyid)
	var tbody = document.getElementById(tbodyid);
	// var tbody = $(tbodyid);
	//   tbody     1 tr
	var trtemplate = tbody.childNodes[1];
	//   tr  td  
	var tds = trtemplate.childNodes;
	//    tr   td     
	var trlength = tds.length;
	var tr = document.createElement("tr");
	for(var i=0;i<trlength;i++){
		var td = document.createElement("td");
		//   td     
		td.className = tds[i].className;
		var content = tds[i].innerHTML;
		td.innerHTML = content;
		tr.appendChild(td);	
	}
	tbody.appendChild(tr);
}
//         
function del(name,tbodyid){
	if(name==null){
		alert("           ");
		return ;
	}
	//     
	var names = document.getElementsByName(name);
	var tbody= document.getElementById(tbodyid);
	//        
	var length = names.length;
	if(length==1){
		alert("         ");
		return ;
	}
	//       
	var arr = new Array();
	try{
		for(var i=0;i<length;i++){
			if(names[i].checked==true){
				//  checkbox    (td)    (tr)      
				arr.push(names[i].parentNode.parentNode);
			}
		}
	}catch(e){
		alert("       :"+e);
	}
	if(arr.length==0){
		alert("         ");
		return ;
	}
	var delsure = confirm("          ?");
	if(!delsure){
		return ;
	}
	for(var j=0;j<arr.length;j++){
		if(arr.length == length){
			alert("         ");
			return ;
		}
		//     prototype_1.5.js      Element.remove(arr[j]);       
		// Element.remove(arr[j]);
		tbody.removeChild(arr[j]);
	}
}
//    ,       
function addItem(tbodyid){
	// 13    
	if(event.keyCode==13){
		add(tbodyid);
	}
}
//     
function checkAll(name){
	//       checkbox  
	var names = document.getElementsByName(name);
	for(var i=0;i<names.length;i++){
		names[i].checked=true;
	}
}
//     --  
function cancelCheck(name){
	var names = document.getElementsByName(name);
	var length = names.length;
	for(var i=0;i<length;i++){
		if(names[i].checked){
			names[i].checked = false;
		}else{
			names[i].checked = true;
		}
	}
}

//     0     :5--5.00,5.5--5.50
function mend(number){
	var str = number+"";
	if (str.indexOf(".")==-1){ //       
		return str+".00";
	}else{ //      
		var tmp = str.substring(str.indexOf(".")+1);
	    if (tmp.length==0) return str+"00";
		if (tmp.length==1) return str+"0";
		if (tmp.length>=2) return str.substring(0,str.indexOf(".")+3);
	}
}

//         
function ForDight(Dight,How) {  
   Dight = Math.round(Dight*Math.pow(10,How))/Math.pow(10,How);  
   return  mend(Dight);  
} 
//          
function showError(str,b){
	//     div
	var div = document.getElementById("div");
	//         
	var strr = "<img src='image/error_m.gif' /><font color='red'>" +str+ "</font>";
	if(b == false){
		div.innerHTML = strr;
		div.style.display = "";
	}else{
		div.style.display = "none";
	}
}
//   (          )
function calc(obj){
	// obj.parentElement           
	var td = obj.parentElement;
	var tr = td.parentElement;
	var numbers = tr.childNodes[4].childNodes[0].value;       //   
	//alert("numbers:"+numbers);
	var faceValue = tr.childNodes[2].childNodes[0].value;     //  
	//alert("faceValue:"+faceValue);
	var additionValue = tr.childNodes[3].childNodes[0].value; //  
	//alert("additionValue:"+additionValue);
	//          
	if(numbers=="" || faceValue=="" || additionValue=="" || isNaN(numbers) || isNaN(faceValue) || isNaN(additionValue)){
		// alert("--      ,        ,           --");
		tr.childNodes[5].childNodes[0].value = "";
		tr.childNodes[6].childNodes[0].value = "";
		tr.childNodes[7].childNodes[0].value = "";
		showError("      ,        ,           ",false);
		return ;
	}else{
		showError("",true);
	}
	//              
	if(numbers<=0*1 || faceValue<=0*1 || additionValue<=0*1 || numbers.indexOf(".")!=-1){
		// alert("--      ,        ,           --");
		tr.childNodes[5].childNodes[0].value = "";
		tr.childNodes[6].childNodes[0].value = "";
		tr.childNodes[7].childNodes[0].value = "";
		showError("      ,        ,           ",false);
		return ;
	}else{
		showError("",true);
	}
	var faceValues = numbers*faceValue;    //    
	var additionValues = numbers*additionValue;//    
	var faceValueAll = faceValues+additionValues;  //    
	//   	
	tr.childNodes[5].childNodes[0].value = ForDight(faceValues,2);
	tr.childNodes[6].childNodes[0].value = ForDight(additionValues,2);
	tr.childNodes[7].childNodes[0].value = ForDight(faceValueAll,2);
}
//          
function calcs(obj){
	// obj.parentElement           
	var td = obj.parentElement;
	var tr = td.parentElement;
	var numbers = tr.childNodes[4].childNodes[0].value;       //   
	var price = tr.childNodes[8].childNodes[0].value;		  //  
	var agio = tr.childNodes[9].childNodes[0].value;		  //  
	if(numbers=="" || price=="" || isNaN(numbers) || isNaN(price) || isNaN(agio)){
		tr.childNodes[10].childNodes[0].value = "";
		showError("          ,           ",false);
		return ;
	}else{
		showError("",true);
	}
	if(numbers<=0*1 || numbers.indexOf(".")!=-1 || price<=0*1){
		tr.childNodes[10].childNodes[0].value = "";
		showError("          ,       ",false);
		return ;
	}else{
		showError("",true);
	}
	var factValues = "" ;
	if(agio==""){//         
		factValues = price*numbers;
		tr.childNodes[10].childNodes[0].value = ForDight(factValues,2);
	}else{
		if(agio<=0*1 || agio>10){
			tr.childNodes[10].childNodes[0].value = "";
			showError("      0  10",false);
			return ;
		}else{
			showError("",true);
			factValues = price*numbers*agio;
			tr.childNodes[10].childNodes[0].value = ForDight(factValues,2);
		}	
	}
	
}

//              
// obj.setAttribute()--                 ,                 
function adds(tbodyid){
	var tbody = document.getElementById(tbodyid);
	var tr = document.createElement("tr");
	
	var td = document.createElement("td");
	var td1 = document.createElement("td");
	var td2 = document.createElement("td");
	var td3 = document.createElement("td");
	var td4 = document.createElement("td");
	var td5 = document.createElement("td");
	var td6 = document.createElement("td");
	var td7 = document.createElement("td");
	var td8 = document.createElement("td");
	var td9 = document.createElement("td");
	var td10 = document.createElement("td");
	var checkbox = document.createElement("<input type='checkbox' name='checkName' />");
	checkbox.setAttribute("align","center");
	
	var input1 = document.createElement("<input name='cardName' type='text' size='12' value='     ' readonly='true' />");
	var input2 = document.createElement("<input name='faceValue' type='text' size='12'  onchange='calc(this);calcs(this)' />");
	var input3 = document.createElement("<input name='additionValue' type='text' size='12'  onchange='calc(this)' />");
	var input4 = document.createElement("<input name='cardNumber' type='text' size='12'  onchange='calc(this);calcs(this)' />");
	var input5 = document.createElement("<input name='faceValues' type='text' size='12' readonly='true' />");
	var input6 = document.createElement("<input name='additionValues' type='text' size='12' readonly='true' />");
	var input7 = document.createElement("<input name='faceValuesAll' type='text' size='12' readonly='true' />");
	var input8 = document.createElement("<input name='price' type='text' size='12'  onchange='calcs(this)' />");
	var input9 = document.createElement("<input name='agios' type='text' size='12'  onchange='calcs(this)' />");
	var input10 = document.createElement("<input name='factValue' type='text' readonly='true' size='12'  onchange='calcs(this)' />");

	td.setAttribute("align","center");
	td1.setAttribute("align","center");
	td2.setAttribute("align","center");
	td3.setAttribute("align","center");
	td4.setAttribute("align","center");
	td5.setAttribute("align","center");
	td6.setAttribute("align","center");
	td7.setAttribute("align","center");
	td8.setAttribute("align","center");
	td9.setAttribute("align","center");
	td10.setAttribute("align","center");
	
	input1.setAttribute("align","center");
	input2.setAttribute("align","center");
	input3.setAttribute("align","center");
	input4.setAttribute("align","center");
	input5.setAttribute("align","center");
	input6.setAttribute("align","center");
	input7.setAttribute("align","center");
	input8.setAttribute("align","center");
	input9.setAttribute("align","center");
	input10.setAttribute("align","center");

	td.appendChild(checkbox);
	td1.appendChild(input1);
	td2.appendChild(input2);
	td3.appendChild(input3);
	td4.appendChild(input4);
	td5.appendChild(input5);
	td6.appendChild(input6);
	td7.appendChild(input7);
	td8.appendChild(input8);
	td9.appendChild(input9);
	td10.appendChild(input10);
		
	tr.appendChild(td);
	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);
	tr.appendChild(td4);
	tr.appendChild(td5);
	tr.appendChild(td6);
	tr.appendChild(td7);
	tr.appendChild(td8);
	tr.appendChild(td9);
	tr.appendChild(td10);

	tbody.appendChild(tr);
}
canc.

<!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=gb2312">
<title>JavaScript  </title>
<script src="js/customer.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<a href="#" onclick="add('DispatchItemTable')">    (    )</a>  
<a href="#" onclick="del('checkName','DispatchItemTable')">  </a>  
<a href="#" onclick="checkAll('checkName')">  </a>  
<a href="#" onclick="cancelCheck('checkName')">  </a>
<a href="#" onclick="adds('DispatchItemTable')">    (     )</a>
      =  *        =  *    
    =    +          =  *  *  
</p>
<table id="table" align='center' border='1' bordercolor='#0690AE' cellpadding='0' cellspacing='0' style='border-collapse:collapse;'  width='100%'>
  <tbody id="DispatchItemTable">
  <tr>
    <td nowrap class='data_list_th'>  </td>
    <td class='data_list_th'>   </td>
    <td class='data_list_th'><font color="red">*</font>  ( )</td>
    <td class='data_list_th'><font color="red">*</font>  ( )</td>
    <td class='data_list_th'><font color="red">*</font>  ( )</td>
    <td class='data_list_th'>    ( )</td>
    <td class='data_list_th'>    ( )</td>
	<td class='data_list_th'>    ( )</td>
	<td class='data_list_th'><font color="red">*</font>  ( )</td>
	<td class='data_list_th'><font color="red">*</font>  </td>
	<td class='data_list_th'>    ( )</td>
  </tr>
  <tr id="testc">
    <td class='data_list_td'><input type="checkbox" name="checkName" /></td>
    <td class='data_list_td'><input name="cardName" type="text" size="12" value="      " readonly="true" onkeydown="addItem('DispatchItemTable')" /></td>
    <td class='data_list_td'><input name="faceValue" type="text" size="12"  onchange="calc(this);calcs(this)" /></td>
    <td class='data_list_td'><input name="additionValue" type="text" size="12"  onchange="calc(this)" /></td>
    <td class='data_list_td'><input name="cardNumber" type="text" size="12"  onchange="calc(this);calcs(this)" /></td>
    <td class='data_list_td'><input type="text" name="faceValues" size="12" readonly="true" /></td>
    <td class='data_list_td'><input type="text" name="additionValues" size="12" readonly="true" /></td>
	<td class='data_list_td'><input type="text" name="faceValuesAll" size="12" readonly="true" /></td>
	<td class='data_list_td'><input type="text" name="price" size="12" onchange="calcs(this)" /></td>
	<td class='data_list_td'><input type="text" name="agios" size="12" onchange="calcs(this)" /></td>
	<td class='data_list_td'><input type="text" name="factValue" size="12" readonly="true" /></td>
  </tr>
</tbody>
</table>
<p><div align="center" id="div" style="display:none;" /></p>
</body>
</html>