简体   繁体   English

如何使用 static 变量制作 js function? 我如何为页面中的每个图像运行这些功能?

[英]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.

相关问题 如何使我的函数在页面运行时立即运行? - How can I make my function to run the moment the page is running? 如何使函数在html页面中自动运行? - How can I make a function run automatically in html page? 流星每次刷新时如何使函数运行? - How to make function run every time page refreshes in meteor? 我如何编写代码使function在react js中每10分钟随机运行一次 - How do I write a code to make a function run randomly every 10 minutes in react js 每次进入组件时,如何使用 useState 使函数运行? - How can I make a function run every time I enter the component, using the useState? 我有由setInterval每3分钟调用一次的JS函数-当页面也加载时如何调用它? - I have the JS function that is invoked every 3 minutes by setInterval - how can I invoke it when page loads too? 每当用户点击页面底部时,如何迭代此函数以进行ajax调用? - How can I iterate this function to make the ajax call every time the user hits the bottom of the page? 在此get函数完全完成并呈现之后,如何确保运行其他函数? - How can I make sure that other functions is run after this get function is fully completed and rendered? 如何将 Express.js 变量传递给 MongoDB 函数? - How can I pass Express.js variables to MongoDB functions? 如何在Typescript中使用静态变量表达构造函数? - How can I express a Constructor Function with static variables in Typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM