[英]How to make js function with static variables ? How can i run these functions for every image in the page?
I have a page that contains a list of photos where users may want to input just a part of the images to be processed in the backend.我有一个包含照片列表的页面,用户可能希望在其中输入要在后端处理的部分图像。
I used CropperJs
to crop the images and it did work but when I provided the variable names manually from the console.我使用
CropperJs
裁剪图像,它确实有效,但是当我从控制台手动提供变量名称时。
here's my functions:这是我的功能:
{% for image in Patient_detail.images.all %}
var $image = $("#image{{image.pk}}");
var cropBoxData;
var canvasData;
$("#modalcrop{{image.pk}}").on("shown.bs.modal", function () {
$image.cropper({
viewMode: 1,
aspectRatio: 1/1,
minCropBoxWidth: 200,
minCropBoxHeight: 200,
ready: function () {
$image.cropper("setCanvasData", canvasData);
$image.cropper("setCropBoxData", cropBoxData);
}
});
}).on("hidden.bs.modal", function () {
cropBoxData = $image.cropper("getCropBoxData");
canvasData = $image.cropper("getCanvasData");
$image.cropper("destroy");
});
// Enable zoom in button
$(".js-zoom-in").click(function () {
$image.cropper("zoom", 0.1);
});
// Enable zoom out button
$(".js-zoom-out").click(function () {
$image.cropper("zoom", -0.1);
});
$(".js-crop-and-upload").click(function () {
var cropData = $image.cropper("getData");
$("#id_x{{image.pk}}").val(cropData["x"]);
$("#id_y{{image.pk}}").val(cropData["y"]);
$("#id_height{{image.pk}}").val(cropData["height"]);
$("#id_width{{image.pk}}").val(cropData["width"]);
$("#formUpload{{image.pk}}").submit();
});
{% endfor %}
If i replace {{image.pk}}
with 53 for example or any valid/existing number the modal with this number as id will work fine so how should I repeat these function with the different pks should I just replace it with classes?如果我将
{{image.pk}}
替换为例如 53 或任何有效/现有数字,则以该数字作为 id 的模态将正常工作,那么我应该如何用不同的 pk 重复这些 function 我应该用类替换它吗?
First of all, did you checked, if {{ image.pk }} is not empty or nullable in this case?首先,您是否检查过 {{ image.pk }} 在这种情况下是否不为空或可为空? Secondly, you override your jQuery method definition, every time when the loop execute.
其次,每次执行循环时,您都会覆盖 jQuery 方法定义。 So for 10 images you get 10 $(".js-zoom-in") definition etc. Try move your loop into jQuery method.
因此,对于 10 张图像,您将获得 10 $(".js-zoom-in")定义等。尝试将您的循环移动到 jQuery 方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.