前提・実現したいこと
いくつかの項目をhtml上で任意に選択し、結果を並べてテキストエリアに表示させたいと思っています。
また、できればその内容をクリップボードにコピーさせるなどもしたいです。
例:
パンを選んでください:プレーン、ゴマ付き、ライ麦
チーズを選んでください:チェダー、アメリカンタイプ、エメンタール、なし
飲み物:あり、なし
自由欄:あいうえお
上記項目をそれぞれ何かしらの方法で選んだ結果…
ライ麦
チェダー
あり
あいうえお
となるようにしたいのです。
ご教授ください。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
ベストアンサー
こんな感じにすればチェックボックスが変化した瞬間にテキストエリアに反映することもできます。
https://jsbin.com/sesiropaka/edit?html,js,output
html
1 <form action="" id="food-form"> 2 <p> 3 パンを選んでください: 4 <label> 5 <input type="radio" name="pan" value="プレーン">プレーン、</label> 6 <label> 7 <input type="radio" name="pan" value="ゴマ付き">ゴマ付き、</label> 8 <label> 9 <input type="radio" name="pan" value="ライ麦">ライ麦</label> 10 </p> 11 <p> 12 チーズを選んでください: 13 <label> 14 <input type="radio" name="tizu" value="チェダー">チェダー、</label> 15 <label> 16 <input type="radio" name="tizu" value="アメリカンタイプ">アメリカンタイプ、</label> 17 <label> 18 <input type="radio" name="tizu" value="エメンタール">エメンタール、</label> 19 <label> 20 <input type="radio" name="tizu" value="なし">なし</label> 21 </p> 22 <p> 23 飲み物: 24 <label> 25 <input type="radio" name="nomi" value="あり">あり、</label> 26 <label> 27 <input type="radio" name="nomi" value="なし">なし</label> 28 </p> 29 <p> 30 自由欄: 31 <input type="text" name="jiyu"> 32 </p> 33 </form> 34 <p> 35 <textarea id="output" rows="10"></textarea> 36 </p> 37 <p> 38 <input type="button" value="copy" id="copy"> 39 </p>
javascript
1/** @type {HTMLFormElement} */ 2var formElement = document.querySelector("#food-form"); 3 4formElement.addEventListener("change", function(e) { 5 var foods = [ 6 formElement["pan"].value || "", 7 formElement["tizu"].value || "", 8 formElement["nomi"].value || "", 9 formElement["jiyu"].value || "", 10 ]; 11 document.querySelector("#output").value = foods.join("\n"); 12}); 13document.querySelector("#copy").addEventListener("click", function (e) { 14 with (document.querySelector("#output")) { 15 selectionStart = 0; 16 selectionEnd = value.length; 17 focus(); 18 } 19 document.execCommand("copy"); 20 alert("クリップボードにコピーしました"); 21}); 22
投稿2018/06/05 09:55
編集2018/06/05 09:57総合スコア216
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/06 02:16
2018/06/06 03:13
2018/06/06 03:16
2018/06/06 05:07
2018/06/06 06:42
2018/06/06 06:47
2018/06/06 07:28
2018/06/06 07:55
0
こんな感じです
javascript
1window.addEventListener('DOMContentLoaded', function(e){ 2 document.querySelector('#check').addEventListener('click',function(e){ 3 var pan=(document.querySelector('[name=pan]:checked')||{value:""}).value; 4 var tizu=(document.querySelector('[name=tizu]:checked')||{value:""}).value; 5 var nomi=(document.querySelector('[name=nomi]:checked')||{value:""}).value; 6 var jiyu=document.querySelector('[name=jiyu]').value; 7 document.querySelector('#output').value=pan+"\n"+tizu+"\n"+nomi+"\n"+jiyu; 8 }); 9 document.querySelector('#copy').addEventListener('click',function(e){ 10 with(document.querySelector('#output')){ 11 selectionStart=0; 12 selectionEnd=value.length; 13 focus(); 14 } 15 document.execCommand('copy'); 16 }); 17});
HTML
1<p> 2パンを選んでください: 3<label><input type="radio" name="pan" value="プレーン">プレーン、</label> 4<label><input type="radio" name="pan" value="ゴマ付き">ゴマ付き、</label> 5<label><input type="radio" name="pan" value="ライ麦">ライ麦</label> 6</p> 7<p> 8チーズを選んでください: 9<label><input type="radio" name="tizu" value="チェダー">チェダー、</label> 10<label><input type="radio" name="tizu" value="アメリカンタイプ">アメリカンタイプ、</label> 11<label><input type="radio" name="tizu" value="エメンタール">エメンタール、</label> 12<label><input type="radio" name="tizu" value="なし">なし</label> 13</p> 14<p> 15飲み物: 16<label><input type="radio" name="nomi" value="あり">あり、</label> 17<label><input type="radio" name="nomi" value="なし">なし</label> 18</p> 19<p> 20自由欄:<input type="text" name="jiyu"> 21</p> 22<p> 23<input type="button" value="check" id="check"> 24</p> 25<p> 26<textarea id="output" rows="10"> 27</textarea> 28</p> 29<p> 30<input type="button" value="copy" id="copy"> 31</p> 32
投稿2018/06/05 08:56
編集2018/06/05 09:04総合スコア114968
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。