上代码:下面的代码会出现俩个文本框焦点不停的互换的问题,死循环。原因我也清楚,想问问大家有没有什么针对这种情况好的解决方案,目前就俩个文本框 ,可能是多个文本框。
<html>
<head>
    <title></title>    <script language="javascript" type="text/javascript">        function CheckValue(senders) {            if (senders.value.length == 0 || senders.value.match(/^\s*$/)) {
                window.alert(senders.id+"不能为空!");
                senders.focus();
                return;
            }        }
    </script></head>
<body>
    <form id="form1" runat="server">
    <center>
        <table>
            <tr>
                <td>
                    Name:
                </td>
                <td>
                    <input type="text" id="txtName" onblur="CheckValue(this);" />
                </td>
            </tr>
            <tr>
                <td>
                    Address:
                </td>
                <td>
                    <input type="text" id="txtAddress"  onblur="CheckValue(this);" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="button" id="btnCommit" value="提交" />
                </td>
            </tr>
        </table>
    </center>
    </form>
</body>
</html>

解决方案 »

  1.   

    <html>
    <head>
      <title></title>  <script language="javascript" type="text/javascript">  function CheckValue(senders) {  if (senders.value.length == 0 || senders.value.match(/^\s*$/)) {
      senders.focus();
      window.alert(senders.id+"不能为空!");
      return false;
      }  }
      </script></head>
    <body>
      <form id="form1" runat="server">
      <center>
      <table>
      <tr>
      <td>
      Name:
      </td>
      <td>
      <input type="text" id="txtName" onblur="CheckValue(this);" />
      </td>
      </tr>
      <tr>
      <td>
      Address:
      </td>
      <td>
      <input type="text" id="txtAddress" onblur="CheckValue(this);" />
      </td>
      </tr>
      <tr>
      <td colspan="2" align="center">
      <input type="button" id="btnCommit" value="提交" />
      </td>
      </tr>
      </table>
      </center>
      </form>
    </body>
    </html>
    或者在onsumit里面再检查
      

  2.   

    IE是这样 FF不这样你可以把这两句话换个位置就行了 其实之所以会死循环 就是因为弹出的提示框 造成了blur事件的触发 这样先focus 再弹应该就不会死了window.alert(senders.id+"不能为空!");
      senders.focus();
    换成:
    senders.focus();
    window.alert(senders.id+"不能为空!");再觉得不妥 那就onsubmit一块验证吧