我想創建一個.txt
使用JavaScript這樣的工作:創建.TXT使用Javascript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$("form").each(function() {
$(this)
})
var textarea = document.querySelector('textarea'); // document.querySelector => Get the first element in the document with tag textarea
var anchor = document.querySelector('a');
anchor.onclick = function() {
anchor.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(textarea.value);
anchor.download = 'export.txt';
};
</script>
<h2>PHP Form Validation Example</h2>
<form>
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Web: <input type="text" name="website">
<br><br>
Kom: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
G:
<input type="radio" name="gender" value="w">w
<input type="radio" name="gender" value="m">m
<br><br>
<input type="submit" name="submit" value="Submit">
<textarea></textarea>
<p><a href="#">Export</a></p>
</form>
瀏覽器下載的數據的用戶投入到該領域的.txt文件。
但是,如果我嘗試做這樣的事情,下載的.txt文件顯示未知的結果。
var input = \t document.getElementsByTagName("input");
var anchor = document.querySelector('a');
anchor.onclick = function() {
anchor.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(input.value);
anchor.download = 'export.txt';
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<h2>PHP Form Validation Example</h2>
<form>
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Web: <input type="text" name="website">
<br><br>
Kom: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
G:
<input type="radio" name="gender" value="w">w
<input type="radio" name="gender" value="m">m
<br><br>
<input type="submit" name="submit" value="Submit">
<textarea></textarea>
<p><a href="#">Export</a></p>
</form>
感謝此工作! 你將如何獲得所有的值(如輸入和textareas ...)? – Ale
適用於Firefox和Chrome,但不適用於IE – Ale
您是否期望我知道您需要IE支持?你測試過哪個版本的IE? –