dongshen7407 2013-06-16 21:42
浏览 284
已采纳

如何在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);
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现