0

我正在使用JCrop来裁剪图像。JCrop 是一个图像裁剪插件。一个页面包含 3 个要裁剪的图像。

这就是我为 jcrop 初始化数组的方式:

var jcrop = [];

然后我有:

$('img.picture').each(function(){
    imgcrop = $(this);
    imgcrop.Jcrop({
        bgColor: 'white',
        aspectRatio: 1
    }, function(){
        jcrop.push(this);
        });
});

问题是,当我拥有jcrop[1].setImage( '/image/no-picture.jpg' );它时,有时会更改第一张或第三张图像。

$('div.deletePhoto a').bind('click', function(e){
    e.preventDefault();
    var index =$('div.deletePhoto').index($(this).parent());

    $.post('/deletePicture', function(data){
        alert(index);
        jcrop[index].setImage( '/image/no-picture.jpg' );
        jcrop[index].disable();
    });
});

变量index可以,但jcrop[index]不是。

HTML:

    <!-- pictures -->
    <div class="blu_3">
        <div class="imgWrapper">
            <img class="blu_10 picture" />
            <div class="delete deletePhoto"><a href="#" title="delete">[ x ]</a></div>
        </div>
        <input class="crop" type="button" value="crop" />
    </div>
    <div class="blu_3">
        <div class="imgWrapper">
            <img class="blu_10 picture" />
            <div class="delete deletePhoto"><a href="#" title="delete">[ x ]</a></div>
        </div>
        <input class="crop" type="button" value="crop" />
    </div>
    <div class="blu_3">
        <div class="imgWrapper">
            <img class="blu_10 picture" />
            <div class="delete deletePhoto"><a href="#" title="delete">[ x ]</a></div>
        </div>
        <input class="crop" type="button" value="crop" />
    </div>

我该如何解决?

4

1 回答 1

0
$('img.picture').each(function(){
    imgcrop = $(this);
    imgcrop.Jcrop({
        bgColor: 'white',
        aspectRatio: 1
    }, function(){

        jcrop.push($(this)); //the $(this) is important

        });
});

$('div.deletePhoto a').bind('click', function(e){
    e.preventDefault();

    var index = $('div.deletePhoto').index($(this).parent()); //return it to this

    $.post('/deletePicture', function(data){
        alert(jcrop[index].attr('id'));
        jcrop[index].setImage( '/image/no-picture.jpg' );
        jcrop[index].disable();
    });
});

html:

 <!-- pictures -->
    <div class="blu_3">
        <div class="imgWrapper">
            <img class="blu_10 picture" id="img_1" />
            <div class="delete deletePhoto"><a href="#" title="delete">[ x ]</a></div>
        </div>
        <input class="crop" type="button" value="crop" />
    </div>
    <div class="blu_3">
        <div class="imgWrapper">
            <img class="blu_10 picture" id="img_2" />
            <div class="delete deletePhoto"><a href="#" title="delete">[ x ]</a></div>
        </div>
        <input class="crop" type="button" value="crop" />
    </div>
    <div class="blu_3">
        <div class="imgWrapper">
            <img class="blu_10 picture" id="img_3" />
            <div class="delete deletePhoto"><a href="#" title="delete">[ x ]</a></div>
        </div>
        <input class="crop" type="button" value="crop" />
    </div>

所以我为每个img标签设置了一个id =“img_1”,id =“img_2”,img =“img_3”,上面的代码检查了它并且它工作正常,每个选择的图像都是正确的id ..

于 2012-10-22T06:39:45.133 回答