Jcrop中的多张图片和prewiev.如何将许多id传递给javascript函数

joh*_*ohn 3 jquery javascript-events jcrop

我在我的应用程序中使用Jcrop插件(Jquery).我决定使用一些ajax解决方案但是将值传递给函数有问题.我不知道是否缺乏JavaScript技能或Jcrop问题.这是代码

jQuery(window).load(function(){

            jQuery('#cropbox').Jcrop({
                onChange: showPreview,
                onSelect: showPreview,
                aspectRatio: 1
            });

        });

        // Our simple event handler, called from onChange and onSelect
        // event handlers, as per the Jcrop invocation above
        function showPreview(coords)
        {
            if (parseInt(coords.w) > 0)
            {
                var rx = 100 / coords.w;
                var ry = 100 / coords.h;

                jQuery('#preview').css({
                    width: Math.round(rx * 500) + 'px',
                    height: Math.round(ry * 370) + 'px',
                    marginLeft: '-' + Math.round(rx * coords.x) + 'px',
                    marginTop: '-' + Math.round(ry * coords.y) + 'px'
                });
            }
        }
Run Code Online (Sandbox Code Playgroud)

这里有一张图片的工作示例: 链接文字

我想要的是将多个参数传递给函数showPreview(coords),如:

        function showPreview(coords,id,size_x,size_y)
        {
            if (parseInt(coords.w) > 0)
            {
                var rx = 100 / coords.w;
                var ry = 100 / coords.h;

                jQuery('#'+id).css({
                    width: Math.round(rx * size) + 'px',
                    height: Math.round(ry * size_y) + 'px',
                    marginLeft: '-' + Math.round(rx * coords.x) + 'px',
                    marginTop: '-' + Math.round(ry * coords.y) + 'px'
                });
            }
        }
Run Code Online (Sandbox Code Playgroud)

但是出现了错误.怎么解决?

小智 13

jQuery('.image_to_crop').each(function() {
  image_to_crop = $(this);
  image_to_crop.Jcrop({
    onChange: function(coords){showPreview(image_to_crop, coords);},
    ...
}

function showPreview(image_to_crop, coords) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

使用此方法,您可以将任何想要的内容传递给showPreview函数(我传递了图像本身,但您可以使用ID或...)