<!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=utf-8" /> 
<title></title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
 <script type="text/javascript"> 
   $(document).ready(function(){ 
   $(".reply").click(function(){ 
     
  $('.af').fadeIn('slow') 
     
  }) 
$(".add").click(function(){ 
var at=$(".replyContent").each(function(){ 
var x=$(this).val(); 
alert(x) 
}) 
}) 
}) 
</script> 
</head> 
<body> 
<div id="container"> 
   <?php 
   for($i=0;$i<3;$i++) 
   { 
?> 
   <div style=" border-bottom:#CCC 1px solid; margin: 0 auto;"> 
   <div> 
   <?=$i?> 
   </div> 
   <div></div> 
   </span> <span class="reply"><a href="#" >回复</a></span> 
   <div style="display:none;" class="af"> 
   <form> 
   <textarea style="width:200px;height:50px; margin:3px 0 0 60px; border:#C90 1px solid;" name="replyContent" class="replyContent"></textarea>
    <div style=" margin-left:150px; margin-top:10px;"> 
   <input value="提交" type="submit" style="padding:2px; border:#C60 1px solid;" class="add" />
    </div> 
   </form> 
   </div> 
   </div> 
   <? 
  } 
?> 
</div> 
<!--end#container--> 
</body> 
</html>            
 
问题:怎样取第一条或第二条的值?each遍历的时候会从上到下遍历 当我点击第一或第二条回复时  总是从第零条开始遍历 我只想取第一条的值 怎样才能避免第零条或第二条的值也给遍历出来了?
 
比如:我点击第3条回复 如果在第零条和第一条的回复框中输入内容的话 总是会先把第零条和第二条的内容也给遍历出来 这样效率比较低 如果不输入内容 头两次alert出来的是空值 怎样避免多余的遍历 直接把第二条中的回复内容取出来 谢谢! 

解决方案 »

  1.   

    <!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=utf-8" />  
    <title></title>  
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
     <script type="text/javascript">  
      $(document).ready(function(){  
      $("span[id^='reply']").click(function(){  
        
      $(this).next(".af").fadeIn('slow')  
        
      })  
    $(".add").click(function(){  
    var at=$(".replyContent").each(function(){  
    var x=$(this).val();  
    alert(x)  
    })  
    })  
    })  
    </script>  
    </head>  
    <body>  
    <div id="container">  
      <?php  
      for($i=0;$i<3;$i++)  
      {  
    ?>  
      <div style=" border-bottom:#CCC 1px solid; margin: 0 auto;">  
      <div>  
      <?=$i?>  
      </div>  
      <div></div>  
      </span> <span class="reply" id="reply<?=$i?>"><a href="#" >回复</a></span>  
      <div style="display:none;" class="af">  
      <form>  
      <textarea style="width:200px;height:50px; margin:3px 0 0 60px; border:#C90 1px solid;" name="replyContent" class="replyContent"></textarea>
      <div style=" margin-left:150px; margin-top:10px;">  
      <input value="提交" type="submit" style="padding:2px; border:#C60 1px solid;" class="add" />
      </div>  
      </form>  
      </div>  
      </div>  
      <?  
      }  
    ?>  
    </div>  
    <!--end#container-->  
    </body>  
    </html>  
      

  2.   

    楼上的 你在第零条 第一条 都输入内容 然后你在点击第二条回复并输入内容 你会发现第零条和第一条的内容也给遍历出来了 如果不输入内容它还是从第零个dom开始遍历 前两个是空值 难道each注定要不断的重复遍历?有没有可能点击第二条直接跳过第零条和第一条 然后取出第零条的内容?
      

  3.   

    肯定可以啊,你先获取当前是第几条比如i
    var length = $(".replyContent").length;
    for(var j = i; j < length; j++){}
    即可。
      

  4.   

    var length = $(".replyContent").length;
    for(var j = i; j < length; j++)
    {var x=$(this).eq(i).val();
    alert(x)
    }这样不行啊 应该怎么写呢?
                                                                  alert(x);
      

  5.   

    不是each循环啊。
    $(".replyContent").eq(i).val()
      

  6.   


     $(".add").click(function(){ var length = $(".replyContent").length;
                                     for(var j = i; j < length; j++)
                                          {
            alert($(".replyContent")[i].value)
                                          }
    })输出的是空值啊
      

  7.   

    $(".add").click(function(){  
    alert($(".replyContent").eq($(".replyContent").length-1).val());})
      

  8.   

    第零条?? 还有这样说的吗? 0,1,2 对应的是第1条,第2条,第3条。解决:甭用each,直接用 alert($(".replyContent")[1].val());就可以了。
      

  9.   

    更正一下,应该是用 alert($(".replyContent").get(1).val());就可以了。