2016-09-06 40 views
0

基本上我有一个表格,里面有一个图像文件输入框,现在我需要显示从文件输入中选择的图片,并在没有表格提交。如何在提交表单之前加载图像

我发现了一个教程,说:

<!DOCTYPE html> 
<html> 
    <head> 
    <script> 
     var reader = new FileReader(); 
     reader.onload = function (e) { 
      $('#blah').attr('src', e.target.result); 
     } 

     function readURL(input) { 
      if (input.files && input.files[0]) { 
       reader.readAsDataURL(input.files[0]); 
      } 
     } 

     $("#imgInp").change(function(){ 
      readURL(this); 
       alert(input.val()); 
     }); 
    </script> 
    </head> 
    <body> 
    <form id="form1" runat="server"> 
     <input type='file' id="imgInp" /> 
     <img id="blah" src="#" alt="your image" /> 
    </form> 

    </body> 
</html> 

但这不显示在我的图像框中选择图像。

回答

3

试试这个样本JS

function readURL(input) { 
 

 
    if (input.files && input.files[0]) { 
 
     var reader = new FileReader(); 
 

 
     reader.onload = function (e) { 
 
      $('#blah').attr('src', e.target.result); 
 
     } 
 

 
     reader.readAsDataURL(input.files[0]); 
 
    } 
 
} 
 

 
$("#imgInp").change(function(){ 
 
    readURL(this); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form id="form1" runat="server"> 
 
    <input type='file' id="imgInp" /> 
 
    <img id="blah" src="#" alt="your image" /> 
 
</form>

0

javascript的问题自然会返回预期的结果。问题在于<script>元素的放置或者不利用.ready()。此外,input未在change事件中定义。您可以将this.value替换为事件处理程序中的input.val()作为参数调用alert()

使用.ready()<script>元剩余为<head>元素的子配售<script>元素下<form>元素在HTML

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script> 
 

 
</head> 
 

 
<body> 
 
    <form id="form1" runat="server"> 
 
    <input type="file" id="imgInp" /> 
 
    <img id="blah" src="#" alt="your image" /> 
 
    </form> 
 
    <script> 
 
    var reader = new FileReader(); 
 
    reader.onload = function(e) { 
 
     $('#blah').attr('src', e.target.result); 
 
    } 
 

 
    function readURL(input) { 
 
     if (input.files && input.files[0]) { 
 
     reader.readAsDataURL(input.files[0]); 
 
     } 
 
    } 
 

 
    $("#imgInp").change(function() { 
 
     readURL(this); 
 
     alert(this.value); 
 
    }); 
 
    </script> 
 
</body> 
 

 
</html>


<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script> 
 
    <script> 
 
    $().ready(function() { 
 
     var reader = new FileReader(); 
 
     reader.onload = function(e) { 
 
     $('#blah').attr('src', e.target.result); 
 
     } 
 

 
     function readURL(input) { 
 
     if (input.files && input.files[0]) { 
 
      reader.readAsDataURL(input.files[0]); 
 
     } 
 
     } 
 

 
     $("#imgInp").change(function() { 
 
     readURL(this); 
 
     alert(this.value); 
 
     }); 
 
    }) 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <form id="form1" runat="server"> 
 
    <input type="file" id="imgInp" /> 
 
    <img id="blah" src="#" alt="your image" /> 
 
    </form> 
 

 
</body> 
 

 
</html>

相关问题