2016-09-06 47 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>

相關問題