dongwo1914 2011-09-08 11:47
浏览 36
已采纳

Jquery,ajax和php

I have this code to present images:

<?php foreach ($images as $row) : ?>      
                    <div class="box col_3">
                        <p><a href="<?php echo base_url() ?>public/images/fullscreen/<?php echo $row['image'] ?>" rel="prettyPhoto[gallery1]" title="<?php echo $row['title'] ?>">
                                <img src="<?php echo base_url() ?>public/images/thumbnails/<?php echo $row['image_thumb'] ?>" title="<?php echo $row['title'] ?>" ></a></p>                            

                                <textarea name="title_image" rows="3" class="title_image"><?php echo $row['title'] ?></textarea>
                                        <input type="hidden" class="id_image" class="id_image"  value="<?php echo $row['id_image'] ?>" >
                                        <input type="submit" name="update_image" id="update_image" value="Update" class="submit" /><br>
                        <a href="#" class="delete" id="<?php echo $row['id_image'] ?>">Delete image</a>
                    </div>
                <?php endforeach; ?>

User can upload an image, and for every image div with class of box is created. This is the code which sends data for update.

$(function(){        
    $('.submit').click(function(){
       ('.box').append('<img src="<?php echo IMG ?>loadinfo.net.gif" id="loading" />');
        var id = $('.id_image').val();            
        var title = $('.title_image').val();         

        $.ajax({
            url: "<?php echo site_url('gallery/update_image') ?>",
            type: 'POST',
            data: 'id=' + id + '&title=' + title,
            success: function(){
                $('#loading').fadeOut(500, function(){
                    $(this).remove();
                });
            }
        });          
         return false;
    });                 
});

This works just for the first box, and for the rest it doesn't. What I need to do to make it work?

  • 写回答

1条回答 默认 最新

  • dongxin2734 2011-09-08 12:07
    关注

    you are referencing css classes within your javascript

    $('.id_image').val()
    

    this will always return the value of the first appearance of this class e.g. the first image. try to traverse through the DOM using your submit button as start e.g.

    $('.submit').click(function(){
    var id = $(this).prevAll('.id_image').val();
    

    this should return the correct value for id. then you can use the same method to get the title

    to update the loading image use the same method, but this time you'll have to use the parents() selector

    $(this).parents('.box').append('<img src="<?php echo IMG ?>loadinfo.net.gif" id="loading" />');
    

    as you are now assigning only a single loading image the removal function should work as expected

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM