0

我在模态窗口上做了一个图像上传器..它工作正常。模态打开并上传图像它保存一张图像并且工作正常!!然后在关闭模态并再次打开它并尝试以模态显示图像时,问题就开始了,因为它将重复的图像保存了 2-4 次随机时间。认为这是因为保存的事件所以尝试了 preventDefault 但没有工作..或者有什么方法可以完全破坏这个 ajax 调用然后可能会起作用。您对解决此问题有何看法?

阿贾克斯

<script type='text/javascript'>
$(document).ready(function() { 
    $('#form').live('change', function(event){
        event.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'uploadajax.php',
            beforeSend: function(){
                $('#throw').html("working..");
            },
            success: function(data){
                $('#form').ajaxForm({                  
                    target: '#throw',
                }).submit();
            }
        });
        e.preventDefault();
        return false;
    });
});

服务器端

<?php
$path = "upload/";

if ($_FILES["image"]["type"] == "") {
    echo 'no file';
} else {

    if ((($_FILES["image"]["type"] == "image/gif")
            || ($_FILES["image"]["type"] == "image/jpeg")
            || ($_FILES["image"]["type"] == "image/jpg")
            || ($_FILES["image"]["type"] == "image/pjpeg")
            || ($_FILES["image"]["type"] == "image/x-png")
            || ($_FILES["image"]["type"] == "image/png"))) {

        if ($_FILES["image"]["type"] == "image/gif") {
            $ext = 'gif';
        }
        if ($_FILES["image"]["type"] == "image/jpeg") {
            $ext = 'jpeg';
        }
        if ($_FILES["image"]["type"] == "image/jpg") {
            $ext = 'jpg';
        }
        if ($_FILES["image"]["type"] == "image/pjpeg") {
            $ext = 'jpeg';
        }
        if ($_FILES["image"]["type"] == "image/x-png") {
            $ext = 'png';
        }
        if ($_FILES["image"]["type"] == "image/png") {
            $ext = 'png';
        }

        $img_url1 = rand(111111, 999999) . time() . substr(str_replace(" ", "_", 9999), 1);

        $img_url1 = $img_url1;

        $tmp = $_FILES['image']['tmp_name'];
        if (move_uploaded_file($tmp, $path . $img_url . "." . $ext)) {



            $link = $path . $img_url;

            $link1 = "$link.$ext";

            unset($tmp);
        }
    } else {
        echo 'Invalid file error';
    }
}
?>

模态窗口

<script type='text/javascript'>
$(document).ready(function() { 
    $('.uploadme').click(function(event){
        event.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'uploadbox.php',
            beforeSend: function(data){
                box.dialog({
                    message: "Setting up upload box",
                    animate: false,
                    title: "<center>Upload Box</center>"
                });
            },
            success: function(data){
                box.hideAll()
                box.dialog({
                    animate: false,
                    message: data,
                    title: "<center>Upload Box</center>"
                });
            }
        });
        return false;
        e.preventDefault();
    });
});

4

0 回答 0