質問編集履歴

3

書式の改善

2019/08/25 13:46

投稿

yys949
yys949

スコア9

test CHANGED
File without changes
test CHANGED
@@ -136,7 +136,9 @@
136
136
 
137
137
 
138
138
 
139
+
140
+
139
- https://qiita.com/KOJI-YAMAMOTO/items/66d0f3b04eb5a9bc131d
141
+ [HTMLとjavascriptを利用してFormの内容をファイルに出力する方法](https://qiita.com/KOJI-YAMAMOTO/items/66d0f3b04eb5a9bc131d)
140
142
 
141
143
  上記サイトが全く同じことをしようとしていたため参考にして上記のjsのコードを以下のように書き換えたのですが、上記サイトのプログラムではきちんとテキストファイルにダウンロードが実行されるのに、書き換えたほうではうまく動きません。
142
144
 

2

コードの修正

2019/08/25 13:46

投稿

yys949
yys949

スコア9

test CHANGED
File without changes
test CHANGED
@@ -134,4 +134,54 @@
134
134
 
135
135
 
136
136
 
137
+
138
+
139
+ https://qiita.com/KOJI-YAMAMOTO/items/66d0f3b04eb5a9bc131d
140
+
141
+ 上記サイトが全く同じことをしようとしていたため参考にして上記のjsのコードを以下のように書き換えたのですが、上記サイトのプログラムではきちんとテキストファイルにダウンロードが実行されるのに、書き換えたほうではうまく動きません。
142
+
143
+
144
+
145
+ ## 変更後のコード
146
+
147
+ ```JavaScript
148
+
149
+ function output() {
150
+
151
+  var flag = 0;
152
+
153
+ //チェックコードは省略
154
+
155
+   if (flag) {
156
+
137
- 関係ないと思いますが入力チェックの部分も入れあります。ご容赦ください。
157
+ window.alert('必須項目全て入力さい。');
158
+
159
+ return false;
160
+
161
+ } else {
162
+
163
+ var text = "'" + document.contact_form.name.value + "','" + document.contact_form.mail.value + "','" + document.contact_form.tel.value + "','" + document.contact_form.comment.value + "','";
164
+
165
+ var a = document.createElement('a');
166
+
167
+ var blob = new Blob([text], {
168
+
169
+ "type": "text/plain;"
170
+
171
+ });
172
+
173
+ if (window.navigator.msSaveBlob) {
174
+
175
+ window.navigator.msSaveBlob(blob, "outFileFromWindows.txt");
176
+
177
+ } else {
178
+
179
+ document.getElementById("createFile").href = window.URL.createObjectURL(blob);
180
+
181
+ }
182
+
183
+ return true;
184
+
185
+ }
186
+
187
+ ```

1

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

2019/08/25 13:44

投稿

yys949
yys949

スコア9

test CHANGED
File without changes
test CHANGED
@@ -56,81 +56,71 @@
56
56
 
57
57
  ```JavaScript
58
58
 
59
- function output(){
59
+ function output() {
60
60
 
61
61
  var flag = 0;
62
62
 
63
- if (document.contact_form.name.value == ""){
63
+ if (document.contact_form.name.value == "") {
64
64
 
65
65
  flag = 1;
66
66
 
67
- document . getElementById( 'notice-input-text-1' ) . style . display = "block";
67
+ document.getElementById('notice-input-text-1').style.display = "block";
68
68
 
69
- }else{
69
+ } else {
70
70
 
71
- document . getElementById( 'notice-input-text-1' ) . style . display = "none";
71
+ document.getElementById('notice-input-text-1').style.display = "none";
72
72
 
73
73
  }
74
74
 
75
-
76
-
77
- if (document.contact_form.mail.value == ""){
75
+ if (document.contact_form.mail.value == "") {
78
76
 
79
77
  flag = 1;
80
78
 
81
- document . getElementById( 'notice-input-text-2' ) . style . display = "block";
79
+ document.getElementById('notice-input-text-2').style.display = "block";
82
80
 
83
- }else{
81
+ } else {
84
82
 
85
- document . getElementById( 'notice-input-text-2' ) . style . display = "none";
83
+ document.getElementById('notice-input-text-2').style.display = "none";
86
84
 
87
85
  }
88
86
 
89
-
90
-
91
- if (document.contact_form.comment.value == ""){
87
+ if (document.contact_form.comment.value == "") {
92
88
 
93
89
  flag = 1;
94
90
 
95
- document . getElementById( 'notice-input-text-3' ) . style . display = "block";
91
+ document.getElementById('notice-input-text-3').style.display = "block";
96
92
 
97
- }else{
93
+ } else {
98
94
 
99
- document . getElementById( 'notice-input-text-3' ) . style . display = "none";
95
+ document.getElementById('notice-input-text-3').style.display = "none";
100
96
 
101
97
  }
102
98
 
99
+ if (flag) {
103
100
 
104
-
105
- if( flag ){
106
-
107
- window . alert( '必須項目は全て入力して下さい。' );
101
+ window.alert('必須項目は全て入力して下さい。');
108
102
 
109
103
  return false;
110
104
 
111
- }else{
105
+ } else {
112
106
 
113
- var text = "'" +
107
+ var text = "'" + document.contact_form.name.value + "','" + document.contact_form.mail.value + "','" + document.contact_form.tel.value + "','" + document.contact_form.comment.value + "','"
114
108
 
115
- document.contact_form.name.value + "','" +
109
+ var a = document.createElement('a');
116
110
 
117
- document.contact_form.mail.value + "','" +
111
+ var blob = new Blob([text], {
118
112
 
119
- document.contact_form.tel.value + "','" +
113
+ "type": "text/plain;"
120
114
 
121
- document.contact_form.comment.value + "','"
115
+ });
122
116
 
123
- var a = document.createElement('a');
117
+ if (window.webkitURL && window.webkitURL.createObject) {
124
118
 
125
- var blob = new Blob([text], { "type": "text/plain;" });
119
+ a.href = window.webkitURL.createObjectURL(blob);
126
120
 
127
- if (window.webkitURL && window.webkitURL.createObject) {
121
+ a.click();
128
122
 
129
- a.href = window.webkitURL.createObjectURL(blob);
130
-
131
- a.click();
132
-
133
- }
123
+ }
134
124
 
135
125
  return true;
136
126
 
@@ -138,13 +128,7 @@
138
128
 
139
129
  }
140
130
 
141
-
142
-
143
-
144
-
145
131
  output()
146
-
147
-
148
132
 
149
133
  ```
150
134