dongshen7407
2013-06-16 21:42
浏览 253

如何在Jquery中动态选择循环生成的多个id并将它们传递给AJAX?

Ok , I am trying to get different id values through Jquery , and pass them to Jquery AJAX that will hit a PHP file so I can get some data back .... I'm not sure how to get all the multiple different ids because Jquery is only getting the first id of many of the unique id values generated by the while loop .. and I would like each unique ID to also be passed to the AJAX function in Jquery .. Your help would be so much appreciated . I'm still new to the Jquery world

<?php
require('../database/connection.php');
?>
        <script type="text/javascript">
          jQuery(document).ready(function() {
           var ID = $('div#opposition img').attr("id"); alert(ID);
           $.ajax({
            type:'GET',
            url :'get_users_images.php',
            data:'screen_name='+ ID,
            success: function(result){
             $('div#opposition img').attr('src', result);
           }
         });

         });

    </script>


    <?php
    $select2  = "SELECT * FROM AUTHORS WHERE ID <> $id";   
    $result2 = mysql_query($select2);
    $result_count = mysql_num_rows($result2);
    echo '<div id ="opposition">';
    while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { 

    echo "<img id ='".$row2['Twitter']."' src='images/ajax-loader.gif' class ='image".$row2['Twitter']."'/>"; //  echos different ids, 
    }
     ?>
    </div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doter1995 2013-06-16 21:46
    已采纳

    You can send an stringified array of id's like this -

    jQuery(document).ready(function () {
        var ID = $('div#opposition img').map(function(){
           return this.id;
        }).get();
    
        $.ajax({
            type: 'GET',
            url: 'get_users_images.php',
            data: { screen_name : JSON.stringify(ID)},
            success: function (result) {
                $('div#opposition img').attr('src', result);
            }
        });
    });
    
    点赞 打赏 评论
  • doufei3152 2013-06-16 21:51

    If I am correct, Actually , why you are placing the set of images returned by php in one img tag, $('div#opposition img').attr('src', result); . Rather I think you must do something like $('div#opposition').innerHTML(result) .

    点赞 打赏 评论

相关推荐 更多相似问题