我想这样:有几个input框,设置class都是"frame1",我就是想用jquery的each循环来判断这些input元素是否都不为空。
HTML:
<tr>
<th valign="top"><span>*</span>联系地址</th>
<td valign="top" id="addressInfo"><input name="txdz" type="text" class="frame1" id="txdz" value="${company.txdz }"/></td>
</tr>

我是这样写的:
 function saveData(){
    $(".frame1").each(function(){
    if(this.value==""){
    var temp = $(this).parent().prev().text();
    var name = temp.substring(1,temp.length);
    alert(name+"不能为空!");
    return false;
    }
    }
    );
                document.form.action="sss.do";    document.form.submit();
   }
我的问题就是现在能判断不为空问题,但是return false之后还是继续会执行下面语句。有没有什么解决方法,或者还有什么更好的方法。

解决方案 »

  1.   


    <script type="text/javascript">
    <!--function saveData(){ 
    var oCount = 0;
      $(".frame1").each(function(){ 
      if(this.value==""){ 
      var temp = $(this).parent().prev().text(); 
      var name = temp.substring(1,temp.length); 
      oCount ++;
      alert(name+"不能为空!"); 
      }

      ) 
      if(oCount <= 0){
      document.form.action="sss.do"; 
      document.form.submit(); 
      }

    //-->
    </script>
      

  2.   

    ...看来只能用变量控制了谢啦。另问为什么<!-- //-->
    把script包起来,有什么讲究吗。好像sytle也有用<!-- //-->
    括起来的,有什么区别啊
      

  3.   

    可以用
    throw new Error('your message');
    把return false;替换掉在each外面
    try{}catch(e){}
    处理
      

  4.   

    记住. $.each实际上也是jquery的自定义函数, 在循环体内return false表示跳出each函数, return true表示跳出这一次循环, 继续执行下一次循环. 所以要是想跳出整个函数, 只有用变量控制.<!--
    //-->
    是防止不支持js的浏览器. 同意4L, 感觉现在都没用了..
      

  5.   

    提供两种 都是用throw的function saveData(){
    try {
    $(".frame1").each(function(){
    if (this.value == "") {
    var temp = $(this).parent().prev().text();
    var name = temp.substring(1, temp.length);
    throw name;
    }
    });
    } catch (e) {
    alert(e + "不能为空!");
    }
    document.form.action = "sss.do";
    document.form.submit();
    }function saveData(){
    var errNames = [];
    try {
    $(".frame1").each(function(){
    if (this.value == "") {
    var temp = $(this).parent().prev().text();
    var name = temp.substring(1, temp.length);
    throw name;
    }
    });
    } catch (e) {
    errNames.push(e);
    }
    if (errNames.length) alert(e.join(',') + "不能为空!");
    else {
    document.form.action = "sss.do";
    document.form.submit();
    }
    }
      

  6.   


    <script type="text/javascript">
    <!--function saveData(){ 
    var oCount = 0;
    var name="";
      $(".frame1").each(function(){ 
          if(this.value==""){ 
              var temp = $(this).parent().prev().text(); 
              if(name=="")
                 name = temp.substring(1,temp.length); 
              oCount ++;
             
          }
        } 
      ) 
      if(oCount > 0){
          alert(name+"不能为空!"); 
      }
      else{
          document.form.action="sss.do"; 
          document.form.submit(); 
    }

    //-->
    </script>
      

  7.   

    修正function saveData(){
        var errNames = [];
        //try {
            $(".frame1").each(function(){
                if (this.value == "") {
                    var temp = $(this).parent().prev().text();
                    var name = temp.substring(1, temp.length);
                    errNames.push(e);
                }
            });
        //} catch (e) {
            //errNames.push(e);
        //}
        if (errNames.length) alert(e.join(',') + "不能为空!");
        else {
            document.form.action = "sss.do";
            document.form.submit();
        }
    }
      

  8.   

    感觉用catch来做还是满不错的,只要在catch里面加个return就够了,谢谢大家。
    感觉jquery很好很强大!