从网址输入预览图像 [英] Preview an image from url input

查看:85
本文介绍了从网址输入预览图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码-从url预览图像-效果很好,除了它仅在用户在输入框外单击时运行. 我如何更改它,以便当在输入框中输入一个值时它将立即运行.我需要立即显示图像. 我想我需要将onblur更改为onchange,但是我尝试将其更改,但它没有用.

I have this code - to preview an image from a url - and it works great, except it only runs when a user clicks outside the input box. How do I change it so it will run straight away when a value is put in the input box. I need the image to show up straight away. I think I need to change onblur to onchange but I tried to change it and it didn't work.

 <input name="input_19" id="input_2_19" type="text" value="" class="medium" placeholder="http://" aria-invalid="false">
    <script> 


     jQuery('#input_2_19').blur(function() {
        var src = jQuery(this).val();

    var previews =  jQuery(".previewImage");
    var drawPreview = true;

    var PreviousSource =  jQuery(this).data('previousSource');

    if(!src.match("^https?://(?:[a-z\-]+\.)+[a-z]{2,6}(?:/[^/#?]+)+\.(?:jpg|gif|png|jpeg)$") && src != "")
    {
          jQuery("#warning").html("Must be an image");
          return false;  
    } else {
         jQuery("#warning").html("");
    }

     jQuery.each(previews , function(index, value) { 
        if (src == "" && PreviousSource == $(value).attr('src'))
        {
              jQuery(value).remove();
             drawPreview = false;
             return false; 
        }
        if( jQuery(value).attr('src') == src)
        {
            drawPreview = false;
            return false;
        }
    });

    if(drawPreview) {
         jQuery('#prev').append('<img class="previewImage" style="max-width:500px;" src="' + src + '">');   
    }
    var previousSource =  jQuery(this).data('previousSource', src);
    });
    </script>

    <div id="warning"></div>
    <div id="prev"></div>

http://jsfiddle.net/W69aA/10/

后续问题 当我添加URL时,图像显示出来.但是,如果我更改URL,那么也会显示另一个图像,并且我有两个图像.如何使它仅显示一张图像.

Follow-up Question When I add a URL the image shows. But if I change the URL then another image shows too and I have two images. How do I make it to only show one image.

为什么该URL显示错误:

Also why is this URL displaying an error: https://ae01.alicdn.com/kf/HTB1q0ucSYrpK1RjSZTEq6AWAVXap/Mifa-Portable-Bluetooth-speaker-Portable-Wireless-Loudspeaker-Sound-System-10W-stereo-Music-surround-Waterproof-Outdoor-Speaker.jpg

推荐答案

您需要触发事件

You need to trigger upon the event oninput and not blur. Here the code you need. You can run here on this snippet. I tried and it works

$('.test').on('input', function() {
  var src = jQuery(this).val();

  var previews = $(".previewImage");
  var drawPreview = true;

  var PreviousSource = $(this).data('previousSource');

  if(!src.match("^https?://(?:[a-z\-]+\.)+[a-z]{2,6}(?:/[^/#?]+)+\.(?:jpg|gif|png|jpeg|webp)$") && src != "") {
    $("#warning").html("Must be an image");
    return false;  
  } else {
    $("#warning").html("");
  }

  $.each(previews , function(index, value) { 
    if (src == "" && PreviousSource == $(value).attr('src')) {
      $(value).remove();
      drawPreview = false;
      return false; 
    }
    if($(value).attr('src') == src) {
      drawPreview = false;
      return false;
    }
  });

  if(drawPreview) {
    $('#prev').append('<img class="previewImage" style="max-width:50px;" src="' + src + '">');   
  }
  
  var previousSource = $(this).data('previousSource', src);
});

.test{
  display:block;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="0" type="text" class="test" />
<input id="1" type="text" class="test"/>
<input id="3" type="text" class="test"/>

<div id="warning"></div>
<div id="prev"></div>

这篇关于从网址输入预览图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆