如何通过jQuery获取同一类的所有值,并插入数组和循环计数在jQuery中是不正确的


How to get all the value of same class by jQuery, and insert into an array and loop count is not correct in jQuery?

这是我的表单

<?php  
        $sql15 = "select * from reviewed_title";
        $query15 = sqlsrv_query( $link, $sql15);
        $count = 1;
        while( $row = sqlsrv_fetch_array( $query15, SQLSRV_FETCH_ASSOC) ) { ?>
        <div class = "title_body">
         <div class = "title_top">
          <div class="form-group">
           <h3><?php 
            $title_id = $row['id'];
            echo $count .".  ".$row['title']; 
            ?>
           </h3>
         </div>
        </div>
        <?php
        $sql16 = "select * from item_reviewed WHERE title_id = $title_id";
                        $count2=1;
         $query16 = sqlsrv_query( $link, $sql16);
         $query17 = sqlsrv_query($link, $sql16, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
         $row_num = sqlsrv_num_rows($query17);
            //$row_num = 5;
         while( $row2 = sqlsrv_fetch_array( $query16, SQLSRV_FETCH_ASSOC) ) { ?>    
        <div class = "title_bottom"
        <?php if($row_num != $count2){?> style="border-bottom: 1px solid #000000;" <?php }?> >
            <div class="form-group">
              <input type="hidden" name="item_reviewed_id" value="<?php echo $row2['id'];?>"  class="item_reviewed_id">
                <div class = "title_bottom_left">
                    <h6> <?php echo $count.".".$count2 .".  ".$row2['items']; 
                                ?></h6>
                </div>
                <div class= "title_bottom_center">
                <label class="col-sm-2 label2" for="email">Y,N,N/A:</label>
                                        <!--<select name="action" class="select <select_<?php echo $row2['id'];?>" id="select_<?php echo $count."".$count2;?>">-->
                                        <select name="action" class="select" >
                                          <option value="">Select</option>
                                        <option value="yes">Yes</option>        
                                      </select>
                                  </div>
                <div class ="title_bottom_right">
                <label class="col-sm-2 label3" for="email">Comment:</label>
                    <!--<textarea name="comment" class="form-control comment commentttt comment_<?php echo $row2['id'];?>>"  <id="comment_<?php echo $count."".$count2;?>"   placeholder="Comment"></textarea>-->
                    <textarea name="comment" class="form-control comment commentttt"    placeholder="Comment"></textarea>
                                </div>
                        </div>
                    </div>
                    <?php $count2++;}?>
                </div>
            <?php  $count++; }?>

这里我得到33个输入字段,33个选择字段和33个隐藏字段。从表item_reviews这里我得到33行。我的jQuery代码是…

var ittem_id = new Array();
    var j = 1;
    $(".item_reviewed_id" ).each(function( i ) {
        ittem_id[j] = $(this).val();
        j++;
        });
        alert(j);

这里我得到1552个值。如何在jQuery中获得33个值?

如果您有1552项,并且该循环只输出33行,那么您必须在页面中使用类.item_reviewed_id的其他元素。使用两个类来确保你选择了正确的元素。JQuery map是映射元素值的一种非常简单的方法

<input type="hidden" name="item_reviewed_id" value="<?php echo $row2['id'];?>"  class="item_reviewed_id thisclass">
var ittem_id = $('.item_reviewed_id.thisclass').map(function(){
     return $(this).val();
});
console.log(ittem_id); //this will be an array with your values!