if(document.formq.test[i].value=="")
应该是把formq改成form1

解决方案 »

  1.   

    你还漏了一个},你用了for和if,但是只有一个}
      

  2.   

    上面各位:
    你们误会了我的意思,我的意思是我通过这样的方式来判断。
    语法错误我也知道,但是不能够判断
    在一个Form里面,我用表格定位来动态生成 一系列的 <input>,
    可能是一个,也可能是多个。
    怎么用JAVASCRIPT来判断它必须为什么输入项,也是判断它的值不能为空。
         for(var i=0;i<document.form1.test.length;i++){
    if(document.form1.test[i].value==""){
       alert("Please select the category!");
       return false;
           break;
             }
       }
      

  3.   

    <form action=about:ok>
    <input type=button value=addInput onclick=addInput()>
    <input type=submit value=submit onclick=return(check())>
    <table border id=t></table>
    </form>
    <script>
    function addInput(){
    t.insertRow().insertCell().innerHTML="<input name=test>"
    }
    function check(){
    var o=document.getElementsByName("test")
    for(var i=0;i<o.length;i++){
    if(o[i].value==""){
       alert("Please select the category!");
       o[i].select();
       return false;
             }
       }
    }
    </script>
      

  4.   

    “document.form1.test[i]”是什么东西啊?
    如果有多个控件同名,用 document.form1.elements("name") 得到这些控件的集合// checkInput 为通用的检查函数
    function checkInput(sFormName, sControlName)
    {
        var oColl = document.forms(sFormName).elements(sControlName);
        for(var i=0;i<oColl.length;i++){
         if(doColl[i].value==""){
         alert("Please select the category!");
         return false;
         }
        }
        return true;
    }// 检查 form1.test
    if ( checkInput("form1", "test") )
    //ok
    else
    //bad
      

  5.   

    if(doColl[i].value==""){==>if(oColl[i].value==""){
      

  6.   

    alexxing(赤铸):
         我按照你的代码写一个小程序来测试,但是提交的时候,总是会提交上去。而不能当返回FALSE的时候就不提交,你帮忙看看如何,多谢了先:
    代码如下:
           <HTML>
    <HEAD>
    <TITLE>test</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function checkInput(sFormName, sControlName)
    {
    var oColl = document.forms(sFormName).elements(sControlName);
    for(var i=0;i<oColl.length;i++){
    if(oColl[i].value==""){
    alert("Please select the category!");
    return false;
    }
    }
    return true;
    }
    // 检查 form1.test
    function check(){
    if (checkInput("form1", "a")){
    return false;
    }
    if (checkInput("form1", "b")){
    return false;
    }
    document.form1.submit();
    }
    //-->
    </SCRIPT>
    </HEAD>
    <BODY bgcolor="#FFFFFF">
    <form name="form1" method="post" action="" >
      <table width="100%" border="1" cellspacing="0" cellpadding="1">
        <tr>
          <td width="31%"> 
            <input type="text" name="a">
          </td>
          <td width="69%"> 
            <select name="b">
              <option>test</option>
              <option value="d">dd</option>
              <option value="d">s</option>
            </select>
          </td>
        </tr>
        <tr>
          <td width="31%"> 
            <input type="text" name="a">
          </td>
          <td width="69%"> 
            <select name="b">
              <option>test</option>
              <option value="d">d</option>
              <option value="dd">ss</option>
            </select>
          </td>
        </tr>
        <tr>
          <td width="31%">&nbsp;</td>
          <td width="69%">&nbsp;</td>
        </tr>
        <tr>
          <td width="31%">&nbsp;</td>
          <td width="69%">
            <input type="button" name="Submit" value="Submit" onclick="return check();">
            <input type="button" name="Submit2" value="Button">
          </td>
        </tr>
      </table>
    </form>
    </BODY>
    </HTML>