跳到主要內容

如何用 JavaScript 讀取 input 裡面的內容?

<!DOCTYPE html>
<html> <head> <title>&lt;input&gt; File Selection</title> <meta http-equiv="X-UA-Compatible" content="IE=10"> </head> <body> <h1>HTML5 &lt;input&gt; File Selection</h1> <h3>Example 1</h3> <input type="file" id="fileSelector" multiple accept="image/*" /> <!-- By design, if you select the exact same files two or more times, the 'change' event will not fire. --> <ul id="fileContentList" style="list-style-type: none;"></ul> <!-- This will be populated with <li> elements via JavaScript. --> <script type="text/javascript"> var message = []; if (!document.getElementById('fileSelector').files) { message = '<p>The ' + '<a href="http://dev.w3.org/2006/webapi/FileAPI/" target="_blank">File API</a>s ' + 'are not fully supported by this browser.</p>' + '<p>Upgrade your browser to the latest version.</p>'; document.querySelector('body').innerHTML = message; } else { document.getElementById('fileSelector').addEventListener('change', handleFileSelection, false); // Add an onchange event listener for the <input id="fileSelector"> element. } function handleFileSelection(evt) { var files = evt.target.files; // The files selected by the user (as a FileList object). if (!files) { msa.alert("<p>At least one selected file is invalid - do not select any folders.</p><p>Please reselect and try again.</p>"); return; } // The variable "files" is an array of file objects. for (var i = 0, file; file = files[i]; i++) { var img_element = document.createElement('img'); // We've only allowed the user to select graphics files, so get ready to display them. img_element.src = window.URL.createObjectURL(file); // Assumes "file" is some sort of graphics file type. img_element.width = 150; // Make all images the same width. img_element.style.verticalAlign = "middle"; // Center the image in the middle of adjacent text. img_element.style.margin = "4px 4px 4px 0"; img_element.onload = function() { window.URL.revokeObjectURL(this.src); } // The file URL is not needed once the file image has been fully loaded. var span_element = document.createElement('span'); span_element.innerHTML = file.name; var li_element = document.createElement('li'); li_element.appendChild(img_element); li_element.appendChild(span_element); document.getElementById('fileContentList').appendChild(li_element); } // for } // handleFileSelection </script> <script src="../utilities.js" type="text/javascript"></script> <!-- Provides the msa.alert() method. --> </body> </html> 在上述範例中,<meta http-equiv="X-UA-Compatible" content="IE=10"> 會指示 Windows Internet Explorer 以 IE10 模式顯示網頁。如需詳細資訊,請參閱定義文件相容性。 script 區塊放在 body 區塊的結尾,一般來說可改善效能,但更重要的是可允許存取先前轉譯的 DOM 元素 (例如 <input type="file" id="fileSelector" multiple accept="image/*" />)。 如果沒有特定的檔案 API 功能,則以 HTML 取代之前的所有標記,警告使用者必要的「檔案 API」功能無法使用。否則,請新增事件接聽程式 (handleFileSelection) 以接聽input 元素上的任何變更。 使用者選取一或多個檔案 (透過 input 元素) 時,會引發 change 事件並叫用 handleFileSelection 函式。傳遞到 handleFileSelection 的事件物件含有使用者所選的檔案清單,也就是 evt.target.files。然後,針對檔案清單中的每個檔案物件,我們會建立影像縮圖清單並顯示相關聯的檔案名稱。

留言

這個網誌中的熱門文章

JSON資料格式

什麼是「JSON」資料格式?      JSON ( J ava S cript O bject N otation)是一種由道格拉斯·克羅克福特構想設計、輕量級的資料交換語言,以文字為基礎,且易於讓人閱讀。 為什麼要統一資料交換的格式?      因為能交換檔案,互相通用。 在JSON之前,主宰網路世界的資料交換格式為XML,XML與JSON的格式有何不同?      JSON和XML的特性差異如下: 1. 描述相同資訊內容情況下,JSON所需儲存容量比XML小,因為它省去XML所需的標籤,加快了資料的傳輸。 2.JSON與XML最大的不同在於XML是一個完整的標記(Markup)語言,造成XML在程式判讀上耗費比較多資源。主要的原因在於XML的設計理念與JSON不同, JSON支援的資料型態包括了字串(String)、數字(Number)、陣列(Array)、布林(Boolean)、物件(Object)、空值(Null)六種。 JSON 與 「JavaScript 的物件」的相同/不同點 在於?      儘管JSON是Javascript的一個子集,但JSON是獨立於語言的文本格式,並且採用了類似於C語言家族的一些習慣。

16進位換算器

這是16進位換算器的流程圖, 假如我輸入"fe",電腦會先把左邊的"f"當成第一個,再判斷等於多少,所以左邊流程圖內的第一個紫色方塊內的result要乘以16加15,因為16進位的第一個數字是16的0次方。後來電腦又發現後面還有數字,所以它又把result乘上16,這時候,result已經是16的1次方了,然後在加14,以此類推。例如:"A7FE3"的10進位是688099 換算 Answer is

設計思考 (Design Thinking)

Design Thinking 101                設計思考也稱作設計思維,設計思考並非專屬於設計師 ; 相反地,設計思考是一種引導創新的方法,它從根本去了解問題的現象,觀察並分析使用者情境並以解決問題,彌補使用者與設計者,在使用、設計產品服務之間的斷層。 每個人只要奠基於創新之上,皆有此種思考取向。設計思考是一個創意解難的過程,透過運用設計思考工具中的的元素。