javascript - Cannot read property '0' of undefined input file in bootstrap module window - Stack Overflow

I am trying to create a form inside a Bootstrap modal. it should contain the input file field and previ

I am trying to create a form inside a Bootstrap modal. it should contain the input file field and preview a chosen image, so I can use Jcrop to crop the image.

So here is what am I doing now:

<script type="text/javascript">
    $('#new-menu').on('shown.bs.modal', function (event) {
        var modal = $(this);

        var src = modal.find(".modal-body .upload");
        var target = modal.find(".image");

        src.bind("change", function () {
            // fill fr with image data
            modal.find(".jcrop-holder").remove();
            readUrl(modal,target,src);
            initJcrop(target);
        });
    });

    function readUrl(modal,target,src){
        if (src.files && src.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                target.attr('src', e.target.result);
            };
            reader.readAsDataURL(input.files[0]);
            initJcrop(target, modal);
        }
        else alert(src.files[0]);
    }
    }


    function showCoords(c) {
        $('#x').val(c.x);
        $('#y').val(c.y);
        $('#w').val(c.w);
        $('#h').val(c.h);
    }

    function initJcrop(img) {
        jcrop_api = $.Jcrop(img);

        jQuery(img).Jcrop({
            aspectRatio: 16 / 9,
            onChange: showCoords,
            setSelect: [0, 90, 160, 0],
            onSelect: showCoords
        }, function () {
            modal.find(".jcrop-holder").css({
                left: "50%",
                marginLeft: -img.width / 2 + "px"
            });
        });
    }
</script>

But i get this error

'Cannot read property '0' of undefined'

HTML

<form action="place/{id}/new/service/">
   <div class="input-group">
       <img src="" class="image"/>
   </div>
   <div class="input-group">
       <span class="input-group-addon" id="basic-addon1">
          <i class="glyphicon glyphicon-apple"></i>
      </span>
       <input type="text" id="form-name" class="form-control"
              placeholder="Назва" value="" aria-describedby="basic-addon1"/>
   </div>
   <div class="input-group">
       <span class="input-group-addon" id="basic-addon2">
          <i class="fa fa-tag"></i>
      </span>
       <input type="number" id="form-price" class="form-control"
              placeholder="Ціна" value="" aria-describedby="basic-addon1"/>
       <span style="padding:2px 5px" class="input-group-addon"><i>.грн</i></span>
   </div>
   <div class="input-group">
       <textarea class="form-control place_description" style="resize: none" rows="5"
                 placeholder="Короткий опис послуги"></textarea>
   </div>
   <div style="text-align: center">
       <small class="description-info">Залишилося 160 символів</small>
   </div>
   <div class="input-group">
       <input type="file" class="upload"/>
   </div>
   <button id="new-service" class="btn btn-primary" type="submit">Зареєструвати</button>
</form>

I am trying to create a form inside a Bootstrap modal. it should contain the input file field and preview a chosen image, so I can use Jcrop to crop the image.

So here is what am I doing now:

<script type="text/javascript">
    $('#new-menu').on('shown.bs.modal', function (event) {
        var modal = $(this);

        var src = modal.find(".modal-body .upload");
        var target = modal.find(".image");

        src.bind("change", function () {
            // fill fr with image data
            modal.find(".jcrop-holder").remove();
            readUrl(modal,target,src);
            initJcrop(target);
        });
    });

    function readUrl(modal,target,src){
        if (src.files && src.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                target.attr('src', e.target.result);
            };
            reader.readAsDataURL(input.files[0]);
            initJcrop(target, modal);
        }
        else alert(src.files[0]);
    }
    }


    function showCoords(c) {
        $('#x').val(c.x);
        $('#y').val(c.y);
        $('#w').val(c.w);
        $('#h').val(c.h);
    }

    function initJcrop(img) {
        jcrop_api = $.Jcrop(img);

        jQuery(img).Jcrop({
            aspectRatio: 16 / 9,
            onChange: showCoords,
            setSelect: [0, 90, 160, 0],
            onSelect: showCoords
        }, function () {
            modal.find(".jcrop-holder").css({
                left: "50%",
                marginLeft: -img.width / 2 + "px"
            });
        });
    }
</script>

But i get this error

'Cannot read property '0' of undefined'

HTML

<form action="place/{id}/new/service/">
   <div class="input-group">
       <img src="http://placehold.it/160x90" class="image"/>
   </div>
   <div class="input-group">
       <span class="input-group-addon" id="basic-addon1">
          <i class="glyphicon glyphicon-apple"></i>
      </span>
       <input type="text" id="form-name" class="form-control"
              placeholder="Назва" value="" aria-describedby="basic-addon1"/>
   </div>
   <div class="input-group">
       <span class="input-group-addon" id="basic-addon2">
          <i class="fa fa-tag"></i>
      </span>
       <input type="number" id="form-price" class="form-control"
              placeholder="Ціна" value="" aria-describedby="basic-addon1"/>
       <span style="padding:2px 5px" class="input-group-addon"><i>.грн</i></span>
   </div>
   <div class="input-group">
       <textarea class="form-control place_description" style="resize: none" rows="5"
                 placeholder="Короткий опис послуги"></textarea>
   </div>
   <div style="text-align: center">
       <small class="description-info">Залишилося 160 символів</small>
   </div>
   <div class="input-group">
       <input type="file" class="upload"/>
   </div>
   <button id="new-service" class="btn btn-primary" type="submit">Зареєструвати</button>
</form>
Share Improve this question edited Jul 23, 2017 at 8:56 joanolo 6,3581 gold badge32 silver badges39 bronze badges asked Oct 29, 2015 at 0:45 Dmitrij KostyushkoDmitrij Kostyushko 6661 gold badge8 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

The src you are passing in function readUrl(modal,target,src) is a jQuery element when you need is to access the DOM element. Have

src.get(0).files && src.get(0).files

Instead of

src.files && src.files[0]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744910292a4600507.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信