最近在用jquery each做了一个关于单项选择题的验证,代码如下
jQuery.each(jQuery("div#select"),function(i,item){
                 selected=0;
                 jQuery.each(jQuery(item).find(":radio"),function(){
                    if(this.checked){selected++;}
                 });
                 if(selected<=0)
                 {
                    $(this).addClass("selectRad");
                    flag=false;
                 }
                else
                {
                    $(this).addClass("not");
                }
                 
            });       
遇到个奇怪的问题,我在本地测试的时候,each能够实现循环,但是当我将网页放在外网上就不能实现循环,只能读取一次,不知道是什么原因,请高手指点下

解决方案 »

  1.   

    jQuery("div#select"),这个只能找到一个id是select的div,怎么循环的那?另外,一般情况下用$("元素").each(function(i){})这样的函数,而不用你写的$.each();$.each(数组, function(i, n){})是对数组进行循环的。注意,上面的两个each中,第一个不带n,第二个带n,在第一个each里,$(this)表示循环中的元素
      

  2.   

    清空一下浏览器缓存再试。至于你的代码,因为没有HTML,没有判断依据。
      

  3.   

    就现在举个最简单的一个的例子:
    jQuery.each(jQuery('div#select'),function (i,item){  
                    alert("i=" + i );  
                });
    html源代码:<div id="select">
      <div class="Title">
         <input type="hidden" name="rpQuestions$ctl01$hideQuestionId" id="rpQuestions_ctl01_hideQuestionId" value="1" />
                                        一、您公司是否&quot;投资&quot;过网络推广服务?</div>
     <div style="padding-left:20px;background-color:#f1f1f1; width:770px;">
                                        <table id="rpQuestions_ctl01_radList" border="0">
    <tr>
    <td><input id="rpQuestions_ctl01_radList_0" type="radio" name="rpQuestions$ctl01$radList" value="A" /><label for="rpQuestions_ctl01_radList_0">A 是</label></td>
    </tr><tr>
    <td><input id="rpQuestions_ctl01_radList_1" type="radio" name="rpQuestions$ctl01$radList" value="B" /><label for="rpQuestions_ctl01_radList_1">B 否</label></td>
    </tr>
    </table></div></div>
                                    <div style="width: 770px; height: 10px; line-height: 10px">
                                    </div>
                                <div id="select">
                                    <div class="Title">
                                        <input type="hidden" name="rpQuestions$ctl02$hideQuestionId" id="rpQuestions_ctl02_hideQuestionId" value="2" />
                                        二、您公司希望通过网络推广起到哪些作用
                                    </div>
                                    <div style="padding-left:20px;background-color:#f1f1f1; width:770px;">
                                        <table id="rpQuestions_ctl02_radList" border="0">
    <tr>
    <td><input id="rpQuestions_ctl02_radList_0" type="radio" name="rpQuestions$ctl02$radList" value="A" /><label for="rpQuestions_ctl02_radList_0">A 提升企业形象</label></td>
    </tr><tr>
    <td><input id="rpQuestions_ctl02_radList_1" type="radio" name="rpQuestions$ctl02$radList" value="B" /><label for="rpQuestions_ctl02_radList_1">B 品牌传播</label></td>
    </tr><tr>
    <td><input id="rpQuestions_ctl02_radList_2" type="radio" name="rpQuestions$ctl02$radList" value="C" /><label for="rpQuestions_ctl02_radList_2">C 产品在线销售</label></td>
    </tr>
    </table></div></div>
    正常现象应该是:每循环一个div为select就弹出序号,
    不正常现象:在本地测试能够正常循环,放在网上就只能循环一次请高手指点啊
      

  4.   

    jQuery.each(jQuery('div#select'),function (i,item){  
      alert("i=" + i );  
      });改成jQuery.each(jQuery('div[name=select]'),function (i,item){  
      alert("i=" + i );  
      });
    试试。
      

  5.   

    不好意思写错了,改为
    jQuery.each(jQuery('div[id=select]'),function (i,item){  
      alert("i=" + i );  
      });
      

  6.   

    十分感谢啊,可以了,我还想问问为什么这样'div#select'选择放在外网就不行啊
      

  7.   

    反正最好一个元素的id不能重复就行了。
    比如
    <input id="test" value="测试1"></input>
    <input id="test" value="测试2"></input>
    用$('#test').length //1
    $('input[id="test"]').length //2
    至于外网内网,我也是跟你一样不知道啊。