【问题标题】:When I upload image files from a folders I cannot make them draggable当我从文件夹上传图像文件时,我无法使它们可拖动
【发布时间】:2016-11-02 13:56:12
【问题描述】:

这是我的主要 html 文件;

<!DOCTYPE html>
<html lang="en">
<head>
<title>Create Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<link rel = "stylesheet"
    type = "text/css"
    href = "jquery-ui-1.11.4.custom/jquery-ui.css " />
<style type = "text/css">
    .dragMe {
    width: auto;
    height: auto;
    /* border: 1px solid blue; */
    text-align: center;
    background-color: white;
    position: absolute;
    display:inline-block;
    z-index: 100;
    }
    .dragMeImage {
    width: 180px;
    height: 55px;
    border: 1px solid blue;
    text-align: center;
    background-color: white;
    position: absolute;
    z-index: 100;
    }
    #target {
    width: 400px;
    height: 600px;
    border: 1px solid red;
    text-align: center;
    position: absolute;
    left: 400px;
    top: 100px;
    z-index: 0;
    }
</style>
<script type = "text/javascript"
    src = "jquery-1.12.4.min.js">
</script>
<script type = "text/javascript"
    src = "jquery-ui-1.11.4.custom/jquery-ui.min.js">
</script>
<script type = "text/javascript" src="ajaxshow.js">
</script>
<script type = "text/javascript">
$(init);
function init(){
    //make all drag me elements draggable
    $(".dragMe").draggable();
    $(".dragMeImage").resizable({
    //aspectRatio: 3.2
    });
    //set target as droppable
    $("#target").droppable();
      //bind events to target
    $("#target").bind("drop", changeTarget);
    $("#target").bind("dropout", resetTarget);
} // end init

function changeTarget(event, ui){
    $("#target").addClass("ui-state-highlight")
    .html("Dropped ")
    .append(ui.draggable.text());
} // end changeTarget

function resetTarget(event, ui){
    $("#target").removeClass("ui-state-highlight")
    .html("Drop on me");
} // end reset
</script>   
</head>
<body id="page2">
<form id=form enctype="multipart/form-data" method="post"         action="show_images.php">
    <input type="submit" value="Continue"/>
</form>
<div id="preview"></div>
<div id="err"></div>
<!-- <h2>Create an Invitation Template to be Sent to the List Owners</h2>
<div class = "dragMe">
<img class = "dragMeImage" src="garbo.png" alt="Garbo Logo"     style="width:180px;height:120px;">
</div>
<div class = "dragMe">
<img class = "dragMeImage" src="CA.png" alt="Garbo Logo"  style="width:180px;height:55px;">
</div>
<div class = "dragMe">
<img class = "dragMeImage" src="CA1.png" alt="Garbo Logo"  style="width:180px;height:55px;">
</div>
<div id = "target">
Drop on this template
</div> -->
</body>
</html> 

这里是 ajaxshow.js;

$(document).ready(function (e) {
 $("#form").on('submit',(function(e) {
  e.preventDefault();
 $.ajax({
     url: "http://localhost/phpexamples/show_images.php",
   type: "POST",
   data:  new FormData(this),
   contentType: false,
         cache: false,
   processData:false,
   beforeSend : function()
   {
    //$("#preview").fadeOut();
    $("#err").fadeOut();
   },
   success: function(data)
      {
    if(data=='invalid file')
    {
     // invalid file format.
     $("#err").html("Invalid File !").fadeIn();
    }
    else
    {
     // view uploaded file.
     $("#preview").html(data).fadeIn();
     $("#form")[0].reset(); 
    }
      },
     error: function(e) 
      {
    $("#err").html(e).fadeIn();
      }          
    });
 }));
});

这里是 PHP 文件;

<?php
$folder = "uploads/";
if(is_dir($folder)){
    if($handle = opendir($folder)){
        while(($file=readdir($handle)) != false){
            if($file==='.' || $file==='..') continue;
            list($width, $height) = getimagesize("uploads/$file");
                $h=150/$width*$height;
                $aspect = $height / $width; 
                    if ($aspect >= 1) $mode = "vertical"; 
                    else $mode = "horizontal";  
                echo  '<div class = "dragMe">';
                echo  '<img  class = "dragMeImage" src="uploads/'.$file.'" width="150" height="$h" alt="">';
                echo  '</div>';
        }
        closedir($handle);
    }
}
?>

当我以静态 HTML(已注释掉)加载图像时,它工作正常。当我运行上面的代码时,它会加载目录中的第一个图像,但它不可拖动或调整大小。当我从 PHP 文件的 echo 部分中取出类 ID 时。它可以正常加载图像,但当然它们不可拖动或调整大小。任何人都可以帮忙。非常感谢。尼尔。

【问题讨论】:

  • 不知道,但您可以检查页面并检查生成的代码是否有任何差异。
  • 你有没有调用过函数init()?
  • @SpencerRohan 第一行:$(init); 在页面加载后调用 init() 作为回调。这也可以写成:$(document).ready(init); 所以是的,他的代码调用了init()

标签: php jquery jquery-ui


【解决方案1】:

由于您是在图片上传后动态添加内容,因此您需要在新元素上调用.draggable()

我建议在您的 AJAX 完成后执行此操作:

$.ajax({
  url: "http://localhost/phpexamples/show_images.php",
  type: "POST",
  data:  new FormData(this),
  contentType: false,
  cache: false,
  processData:false,
  beforeSend : function() {
    $("#err").fadeOut();
  },
  success: function(data) {
    if(data=='invalid file') {
      // invalid file format.
      $("#err").html("Invalid File !").fadeIn();
    } else {
      // view uploaded file.
      $("#preview").html(data).fadeIn();
      $("#form")[0].reset();
      $("#preview").find(".dragMe").draggable();
    }
  },
  error: function(e) {
    $("#err").html(e).fadeIn();
  }          
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-11
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多