现有input n个,id分别为a,b,c,d...
如果不限制就可以随意输入,但现要求
排在前面的必须先输入,函数该怎么写比较好。

解决方案 »

  1.   

    要设全局变量吧?或是弄个display:none的记录下?
      

  2.   

    下面的代码 ie6 ie7 firefox测试通过<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    window.onload=function(){TBox=document.getElementsByTagName("input")}//获取input集合
    function aa(obj)
    {
    for(i=0;i<TBox.length;i++ )//循环每一个input
    {
    if(TBox[i].id==obj.id)//取得当前鼠标选中项
    {
    if(i!=0)//不是第一项
    {
    if(TBox[i-1].value=="")//检查鼠标选中项的上面一项是否为空
    {
    aa(TBox[i-1])//去检查鼠标选中项的上面的一项 重复执行
    }
    else
    {
    TBox[i].onfocus="";//检查鼠标选中项的上面一项不为空 清空focus
    TBox[i].focus();//检查鼠标选中项的上面一项不为空 选中input获取焦点
    }
    }
    else
    {
    TBox[0].focus();//是第一项就要获取焦点
    }

    }
    }
    }
    </script>
    </head><body>
    <form id="form1" name="form1" method="post" action="">
        <p>
      <input type="text" name="a" id="a"  />
      </p>
      <p>
        <input type="text" name="b" id="b" onfocus="aa(this)" />
      </p>
      <p>
        <input type="text" name="c" id="c" onfocus="aa(this)"/>
      </p>
      <p>
        <input type="text" name="e" id="e" onfocus="aa(this)" />
      </p>
      <p>
        <input type="text" name="f" id="f" onfocus="aa(this)"/>
      </p>
      <p>
        <input type="text" name="g" id="g" onfocus="aa(this)" />
      </p>
      <p>
        <input type="text" name="h" id="h" onfocus="aa(this)"/>
      </p></p>
    </form>
    </body>
    </html>
      

  3.   

    <!DOCTYPE  html  PUBLIC  "-//W3C//DTD  XHTML  1.0  Transitional//EN "  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
    <html  xmlns= "http://www.w3.org/1999/xhtml "> 
    <head> 
    <meta  http-equiv= "Content-Type "  content= "text/html;  charset=gb2312 "  /> 
    <title>无标题文档 </title> 
    <style></style>
    <script>
    function change(){
    var ia = document.getElementById("a");
    var ib = document.getElementById("b");
    var ic = document.getElementById("c");
    var id = document.getElementById("d");

    if(ia.value==""){
    ib.disabled = 'disable';
    }else{
    ib.disabled = '';
    }
    if(ib.value==""){
    ic.disabled = 'disable';
    }else{
    ic.disabled = '';
    }
    if(ic.value==""){
    id.disabled = 'disable';
    }else{
    id.disabled = '';
    }
    }
    </script>
    </head> 
    <body>
    <input type="text" id="a" onkeyup="change()" /><br />
    <input type="text" id="b" onkeyup="change()" disabled="disabled" /><br />
    <input type="text" id="c" onkeyup="change()" disabled="disabled" /><br />
    <input type="text" id="d" disabled="disabled" /><br />
    </body> 
    </html>
      

  4.   


    <script>
    function chk(id){
      num=id.match(/\d+/g)
      if(num==1){return;}  var preID='a'+(num-1);
      var o=document.getElementById(preID);
      if(o.value.length==0){
        alert("请先填写上一个输入框!");o.focus();
      }
    }
    </script>
    <input type="text" name="a" id="a1" onkeydown='chk(this.id);'><br>
    <input type="text" name="b" id="a2" onkeydown='chk(this.id);'><br>
    <input type="text" name="c" id="a3" onkeydown='chk(this.id);'><br>
    <input type="text" name="d" id="a4" onkeydown='chk(this.id);'><br>