简体   繁体   English

希望在我创建的自定义表单框中直观地加载图片(也可以删除)

[英]Would like to have a picture visually load in a custom form box I have created (also able to delete)

this one may be a little bit of a long one. 这可能有点长。

I have a form that takes a picture upload that I have customized with CSS to appear as a box. 我有一个表单,该表单需要使用CSS定制的图片上传,显示为一个框。 I would like it so that when the user clicks the box and are prompted to upload a picture, they can choose one and have it appear in the box (where the box will lengthen to fit the picture...the picture itself will be resized to fit the width, i have this part handled). 我希望这样,当用户单击该框并提示上传图片时,他们可以选择一个并将其显示在框中(其中框会加长以适合图片...图片本身将被调整大小以适应宽度,我已经处理了这部分)。

I am trying to make this happen with javascript, ajax, etc...: 我正在尝试使用javascript,ajax等来实现这一点:

I am including these scripts: 我包括以下脚本:

<link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" media="screen" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
<script type="text/javascript" src="/js/script.js"></script>

as well as the code found here: 以及在此处找到的代码:

http://homesconnect.me/js/jquery.form.js

script.js contains: script.js包含:

function validate_picture(form_data, form) {
    var form = form[0];
    if ( ! form.picture.value)
        return false; 
}

function show_picture_loader() {
    $('.add_picture_label').hide().before('<img src="/images/loader.gif" class="loader"/>');
}

function hide_picture_loader() {
    $('.loader').remove();
    $('.add_picture_label').show();
}

function show_error_message(form_obj, message) {
    $('.error').remove();

    if(message != '')
    {
        form_obj.before('<div class="notification_msg error">' + message + '</div>');
        $('html,body').animate({scrollTop: form_obj.parent().offset().top});
    }
}

I also have the live script in the page itself written as so: 我还在页面本身中编写了实时脚本,如下所示:

   <script>
    $(document).ready(function(){

    $('.upload_picture_form').ajaxForm({

dataType: 'json', beforeSubmit: show_picture_loader, success: function(data) {
                                                         $('.pictures').prepend('<div class="profile_picture"><div class="delete_picture"><span></span></div><img src="' + data.image + '" /></div>');
                                                         $('form.account_form').prepend('<input type="hidden" name="image[]" value="' + data.image + '" />');
                                                         hide_picture_loader();
                                                         }
                                                         });

                      $('input.upload_picture_file_input').change(function(e) {
                                                                  $('.upload_picture_form').submit();
                                                                  });

                      $('.delete_picture span').live('click', function(e) {
                                                     $(this).closest('.profile_picture').fadeOut();
                                                     $.get('delete_picture', {image: $(this).closest('.profile_picture').find('img').prop('src')});
                                                     });
                      });
    </script> 

The form code is as so: 表单代码如下:

<div class="pictures add_pictures">
                <div class="add_picture">
                    <div class="upload_picture">
                        <form action="upload.php" method="POST" enctype="multipart/form-data" name="upload_picture_form" class="upload_picture_form">
                            <span class="add_picture_label">+ Add a Profile Picture</span>
                            <input type="file" name="upload_picture_fileinput" class="upload_picture_file_input"/>
                            <input type="hidden" name="MAX_FILE_SIZE" value="100000"/>
                            <br><br><br><br><br><br><br>

                            <input type="submit" id="submit" value="Upload" />
                        </form>
                    </div>
                </div>
            </div>

Upload.php takes the image uploaded and saves it in a directory on my server, as well as saving its filename in a mysql directory. Upload.php获取上传的图像并将其保存在我的服务器上的目录中,并将其文件名保存在mysql目录中。 This works fine. 这很好。 However, the javascript I have included are not making the image appear in the box, and hence I am not able to delete it or anything either. 但是,我包含的javascript并没有使图像显示在框中,因此我无法删除它或任何其他内容。

Here's the relevant CSS if it matters: 如果需要的话,以下是相关的CSS:

    .content .pictures {
    padding: 5px;
    background: #fff;
    width: 350px;
    float: right;
    box-shadow: 0 1px 10px rgba(0,0,0,.2);
}

.content .pictures .profile_picture img {
    margin: 0;
    padding: 0;
    width: 350px;
}

.content .pictures .profile_picture {
    position: relative;
}

.content .pictures .profile_picture .delete_picture {
    position: absolute;
    right: 0;
    top: 0;
    display: none;
}

.content .pictures .profile_picture .delete_picture span {
    padding: 0;
    font: bold 12px/normal 'Helvetica', 'Arial', sans-serif;
    color: #444;
    cursor: pointer;
    background: url(/images/close.png) no-repeat;
    width: 30px;
    height: 29px;
    display: block;
}

.content .pictures .profile_picture:hover > .delete_picture {
    display: block;
}

If you feel like you see a problem or can help I would be very appreciative. 如果您觉得自己有问题或可以帮忙,我将不胜感激。 I know this one is a doozy ha. 我知道这是一只傻瓜哈。 Thanks. 谢谢。

ps also is there a way that I can remove the "update" button, and have it so that the image is uploaded automatically when the it is chosen by the user (as opposed to having them hit a button to do so)? ps还有一种方法,我可以删除“更新”按钮,并使用它,以便在用户选择图像时自动上传图像(而不是让他们按按钮这样做)?

EDIT: I may have misunderstood the question. What I interpreted was an effect like that of Newgrounds.com's text boxes, which have a picture inside the text boxes, a neat aesthetic. If I have misinterpreted the question, I apologize

in css, you can put 在CSS中,你可以把

try this: 尝试这个:

textarea#image { background-image:url('image.jpg'); width:600; height:400 }

in this simple css example, assuming you named your input " <textarea id="image"></textarea> ", the document puts a background image inside the element. 在这个简单的CSS例子中,假设您将输入命名为“ <textarea id="image"></textarea> ,文档将在元素内放置一个背景图像。

Because of jQuery's CSS affinity, you might be able to achieve the same result using just jQuery. 由于jQuery具有CSS相似性,因此仅使用jQuery就可以实现相同的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 已为单个图像创建了点击效果,但希望其他图像具有相同的效果 - Have created a click effect for a single image, but would like to have the other images have the same effect 我已经创建了一个复选框,动态地希望在提交表单之前验证该复选框 - i have created a check box dynamicaly want to validate the checkbox before submiting form 我创建的网络表单存在问题 - I have an issue with a web form I have created 在 javascript 验证后,我无法加载我作为学生创建的另一个 html 页面。html - I am not able to load another html page which I have created as student.html after javascript validation 我有JSON数据,我想在Full Calender上显示 - I have JSON data and I would like to show that on Full Calender 我有一个选项表单,我想为我选择的选项标签的每个元素显示来自 JSON 文件的不同图像 - I have an option form and I would like to show a different image from a JSON file for each element of the option tag i choose 对于一系列图片,我希望每个图像都有不同的音频 - With an array of pictures I would like to have a different audio for each image 我想在我的网络中放置一个三角形并用图像填充它 - I would like to have a triangle in my web and filled it with images 我在提交表单和选项卡更改时遇到问题 - i have aproblem with submitting a form and also tab change 我怎么能删除我在javascript中创建的特定元素? - How could I delete a specific element that I have created in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM