2

我刚刚发布了一个类似的问题,以发现这不是我所需要的。我很抱歉。

这是我正在使用的代码:

jQuery(document).ready(function($) { 

  $('.upload_button').click(function() {

    targetfield = $(this).prev('.logo_url');
    tb_show('', 'media-upload.php?type=image&TB_iframe=true');

    window.send_to_editor = function(html) {
      imgurl = $('img',html).attr('src');
      jQuery(targetfield).val(imgurl);
      tb_remove();

      $('.upload_preview img').attr('src',imgurl); // THIS IS WHERE I'M HAVING PROBLEMS

      $('#submit_options_form').trigger('click');
    };

    return false;

  });
});

我对 jQuery 不是最好的,而且对某些东西还是很陌生。我需要在预览类中填充 img 标记的 url,但仅限于获取 url 的那个。我现在拥有的将填充具有该类的每个图像标签。任何帮助是极大的赞赏。

我需要它来填充它,就像填充输入字段一样。

编辑

这是一些示例 HTML。我在一个页面上使用多个这些。

<li class="slide">

            <input class="logo_url" name="theme_wptuts_options[logo]" value="src[]" />
                <input class="upload_button" type="button" class="button" value="<?php _e( 'Upload Logo' ); ?>" />

                <div class="upload_preview" style="min-height: 100px;">
                    <img style="max-width:100%;" src="src[]" />
                </div>

            <button class="remove_slide button-secondary">Remove This Slide</button>

</li>
4

4 回答 4

2

使用 :eq(0),它将选择第一个元素,恕我直言,与 .first() .last() 等相比,这是最好的方法 :)

$('.upload_preview img:eq(0)').attr('src',imgurl);
于 2012-11-06T00:55:59.057 回答
1

imgurl* 已编辑 2012 年 11 月5 日晚上 9:47 ET *

这可能是它!我无法在小提琴中进行测试,因为我无法在小提琴中执行 window.send 功能。

修订工作小提琴

jQuery

var whichLi;

jQuery(document).ready(function() {

    $('.upload_button').click(function() {

       whichLi= $(this).parent('li');

       targetfield = $(this).prev('.logo_url');
        tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');

        window.send_to_editor = function(html) {


            imgurl = $('img', html).attr('src');
            jQuery(targetfield).val(imgurl);
            tb_remove();

            $(whichLi).find('img').attr('src', imgurl);
            $('#submit_options_form').trigger('click');
        };

        return false;

    });
});
于 2012-11-06T00:53:20.320 回答
1

经过大量的反复试验,这就是我最终得到的结果,就像我想要的那样。我感谢大家的帮助和回答。

jQuery(document).ready(function($) { 
$('.upload_button').click(function() {
     targetfield = $(this).prev('.logo_url');
     imgpreview = $(this).parent().find('img');
     tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
window.send_to_editor = function(html) {
     imgurl = $('img',html).attr('src');
     jQuery(targetfield).val(imgurl);
     tb_remove();
     $(imgpreview).attr('src', imgurl);
     $('#submit_options_form').trigger('click');
};
return false;
});
});
于 2012-11-06T02:51:11.917 回答
0

:first选择器做的工作:

$('.upload_preview:first img').attr('src', imgurl); 
于 2012-11-06T00:22:15.500 回答