質問するログイン新規登録

質問編集履歴

3

書式の改善

2019/08/25 13:46

投稿

yys949
yys949

スコア9

title CHANGED
File without changes
body CHANGED
@@ -67,7 +67,8 @@
67
67
  ```
68
68
 
69
69
 
70
+
70
- https://qiita.com/KOJI-YAMAMOTO/items/66d0f3b04eb5a9bc131d
71
+ [HTMLとjavascriptを利用してFormの内容をファイルに出力する方法](https://qiita.com/KOJI-YAMAMOTO/items/66d0f3b04eb5a9bc131d)
71
72
  上記サイトが全く同じことをしようとしていたため参考にして上記のjsのコードを以下のように書き換えたのですが、上記サイトのプログラムではきちんとテキストファイルにダウンロードが実行されるのに、書き換えたほうではうまく動きません。
72
73
 
73
74
  ## 変更後のコード

2

コードの修正

2019/08/25 13:46

投稿

yys949
yys949

スコア9

title CHANGED
File without changes
body CHANGED
@@ -66,4 +66,29 @@
66
66
  output()
67
67
  ```
68
68
 
69
+
70
+ https://qiita.com/KOJI-YAMAMOTO/items/66d0f3b04eb5a9bc131d
71
+ 上記サイトが全く同じことをしようとしていたため参考にして上記のjsのコードを以下のように書き換えたのですが、上記サイトのプログラムではきちんとテキストファイルにダウンロードが実行されるのに、書き換えたほうではうまく動きません。
72
+
73
+ ## 変更後のコード
74
+ ```JavaScript
75
+ function output() {
76
+  var flag = 0;
77
+ //チェックコードは省略
78
+   if (flag) {
69
- 関係ないと思いますが入力チェックの部分も入れあります。ご容赦ください。
79
+ window.alert('必須項目全て入力さい。');
80
+ return false;
81
+ } else {
82
+ var text = "'" + document.contact_form.name.value + "','" + document.contact_form.mail.value + "','" + document.contact_form.tel.value + "','" + document.contact_form.comment.value + "','";
83
+ var a = document.createElement('a');
84
+ var blob = new Blob([text], {
85
+ "type": "text/plain;"
86
+ });
87
+ if (window.navigator.msSaveBlob) {
88
+ window.navigator.msSaveBlob(blob, "outFileFromWindows.txt");
89
+ } else {
90
+ document.getElementById("createFile").href = window.URL.createObjectURL(blob);
91
+ }
92
+ return true;
93
+ }
94
+ ```

1

ご指摘いただきました点について修正しました。

2019/08/25 13:44

投稿

yys949
yys949

スコア9

title CHANGED
File without changes
body CHANGED
@@ -27,51 +27,43 @@
27
27
  ```
28
28
 
29
29
  ```JavaScript
30
- function output(){
30
+ function output() {
31
31
  var flag = 0;
32
- if (document.contact_form.name.value == ""){
32
+ if (document.contact_form.name.value == "") {
33
33
  flag = 1;
34
- document . getElementById( 'notice-input-text-1' ) . style . display = "block";
34
+ document.getElementById('notice-input-text-1').style.display = "block";
35
- }else{
35
+ } else {
36
- document . getElementById( 'notice-input-text-1' ) . style . display = "none";
36
+ document.getElementById('notice-input-text-1').style.display = "none";
37
37
  }
38
-
39
- if (document.contact_form.mail.value == ""){
38
+ if (document.contact_form.mail.value == "") {
40
39
  flag = 1;
41
- document . getElementById( 'notice-input-text-2' ) . style . display = "block";
40
+ document.getElementById('notice-input-text-2').style.display = "block";
42
- }else{
41
+ } else {
43
- document . getElementById( 'notice-input-text-2' ) . style . display = "none";
42
+ document.getElementById('notice-input-text-2').style.display = "none";
44
43
  }
45
-
46
- if (document.contact_form.comment.value == ""){
44
+ if (document.contact_form.comment.value == "") {
47
45
  flag = 1;
48
- document . getElementById( 'notice-input-text-3' ) . style . display = "block";
46
+ document.getElementById('notice-input-text-3').style.display = "block";
49
- }else{
47
+ } else {
50
- document . getElementById( 'notice-input-text-3' ) . style . display = "none";
48
+ document.getElementById('notice-input-text-3').style.display = "none";
51
49
  }
52
-
53
- if( flag ){
50
+ if (flag) {
54
- window . alert( '必須項目は全て入力して下さい。' );
51
+ window.alert('必須項目は全て入力して下さい。');
55
52
  return false;
56
- }else{
53
+ } else {
57
- var text = "'" +
58
- document.contact_form.name.value + "','" +
59
- document.contact_form.mail.value + "','" +
60
- document.contact_form.tel.value + "','" +
61
- document.contact_form.comment.value + "','"
54
+ var text = "'" + document.contact_form.name.value + "','" + document.contact_form.mail.value + "','" + document.contact_form.tel.value + "','" + document.contact_form.comment.value + "','"
62
- var a = document.createElement('a');
55
+ var a = document.createElement('a');
56
+ var blob = new Blob([text], {
63
- var blob = new Blob([text], { "type": "text/plain;" });
57
+ "type": "text/plain;"
58
+ });
64
- if (window.webkitURL && window.webkitURL.createObject) {
59
+ if (window.webkitURL && window.webkitURL.createObject) {
65
- a.href = window.webkitURL.createObjectURL(blob);
60
+ a.href = window.webkitURL.createObjectURL(blob);
66
- a.click();
61
+ a.click();
67
- }
62
+ }
68
63
  return true;
69
64
  }
70
65
  }
71
-
72
-
73
66
  output()
74
-
75
67
  ```
76
68
 
77
69
  関係ないとは思いますが入力チェックの部分も入れてあります。ご容赦ください。