dimanche 19 août 2018

why did not display jquery image change preview in the laravel?

in My Laravel 5.6 app I have some image changing using jquery. this is my blade file.

<img id="preview"
                         src=""
                         height="200px" width="200px"/>
                    <input class="form-control" style="display:none" name="image" type="file" id="image" name="_token" value="">
                    <br/>
                    <a href="javascript:changeProfile();">Add Image</a> |
                    <a style="color: red" href="javascript:removeImage()">Delete</a>
                    <input type="hidden" style="display: none" value="0" name="remove" id="remove">

and this is My jquery code for images changing.

 <script>
        function changeProfile() {
            $('#image').click();
        }
        $('#image').change(function () {
            var imgPath = $(this)[0].value;
            var ext = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
            if (ext == "gif" || ext == "png" || ext == "jpg" || ext == "jpeg")
                readURL(this);
            else
                alert("Please select image file (jpg, jpeg, png).")
        });
        function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
                reader.readAsDataURL(input.files[0]);
                reader.onload = function (e) {
                    $('#preview').attr('src', e.target.result);
                    $('#remove').val(0);
                }
            }
        }
        function removeImage() {
            $('#preview').attr('src', 'http://localhost:8000/images/noimage.png');
            $('#remove').val(1);
        }
    </script>

but when I click Add Image button and select new image a new image preview is not displaying.... how can fix this problem???



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire