当前位置: 代码迷 >> Web前端 >> 序号掉换
  详细解决方案

序号掉换

热度:180   发布时间:2012-08-22 09:50:35.0
序号置换
<html>
<title>
</title>
<head>
</head>
<body>
<table>
<tbody id="tbody">
<tr>
<td>
名称
</td>
<td>
序号
</td>
</tr>
</tbody>
</table>
</body>
</html>
<script type="text/javascript">
function onload(){
   aa();
   for(var i = 1;i <= 4;i++){
   var sel_i = document.getElementById("select"+i);

       for(var j = 1;j < 5;j++){
sel_i.options[sel_i.options.length] = new Option(j,j);
}
sel_i.options[i-1].selected = true;
document.getElementById("h"+i).value=sel_i.options[i-1].value;
   }
}

function aa() {
     
     var root = document.getElementById("tbody")
for(var i = 1;i <= 4;i++){
var newRow_i = root.insertRow();
var newCell_i = newRow_i.insertCell();
var newCell_i_i = newRow_i.insertCell();
var newCell_i_i_i = newRow_i.insertCell();
newCell_i.innerHTML = "a"+i;
newCell_i_i.innerHTML = '<select name="select'+i+'" id="select'+i+'" onchange="changeIndex(this.value,'+i+');"></select>';
newCell_i_i_i.innerHTML = '<input type="hidden" name="h'+i+'" id="h'+i+'">';
    }
}
function changeIndex(val,onLine){
   var oldValue = document.getElementById('h'+onLine).value;
   var newValue = val;
   for(var i = 1;i <= 4;i++){
if(document.getElementById('h'+i).value == newValue){
document.getElementById('select'+i).selectedIndex=oldValue-1;
document.getElementById('h'+i).value =oldValue;
document.getElementById('h'+onLine).value = newValue;
         return;
}
  }
}
onload();
</script>
   
  相关解决方案