###前提・実現したいこと
<select>内の<option>を自動で追加してさらにセレクトボタンを選択したときに選択した数だけ<input>を追加するプログラムを作っています。
たとえば、セレクトボタンの20を押した場合<input>を20個追加するというものです。
<select>内の<option>を自動で追加するプログラム、セレクトボタンを選択したときに<input>を追加するプログラムはそれぞれできているのですが、それらを合わせたプログラムを作ろうとするとうまくいきません。
やり方を教えてはいただけないでしょうか。
あと、欲を言えばセレクトボタンを選択したときに<input>を追加するプログラムも自動化してスリムにしたいです。
ご指導、ご鞭撻のほどよろしくお願いします。
###ソースコード
javascript
1//<select>内の<option>を自動で追加する 1-50 2$(function(){ 3 $("#select_number").empty(); 4 //select内の設定 5 for(i=0; i<50; i++){ 6 $('<option value="' + i + '">' + (i+1) + '</option>').appendTo('#select_number') 7 } 8}); 9//セレクトボタンを選択したときに選択した数だけ<input>を追加する 10$("#select_number").on("change", function(){ 11 $("#list_name").empty(); 12 switch(){ 13 case "0": 14 $('<input type="text" id="name1"').appendTo("#list_name"); 15 break; 16 case "1": 17 $('<input type="text" id="name1"').appendTo("#list_name"); 18 $('<input type="text" id="name2"').appendTo("#list_name"); 19 break; 20 case "2": 21 $('<input type="text" id="name1"').appendTo("#list_name"); 22 $('<input type="text" id="name2"').appendTo("#list_name"); 23 $('<input type="text" id="name3"').appendTo("#list_name"); 24 break; 25 case "3": 26 $('<input type="text" id="name1"').appendTo("#list_name"); 27 $('<input type="text" id="name2"').appendTo("#list_name"); 28 $('<input type="text" id="name3"').appendTo("#list_name"); 29 $('<input type="text" id="name4"').appendTo("#list_name"); 30 break; 31 case "4": 32 $('<input type="text" id="name1"').appendTo("#list_name"); 33 $('<input type="text" id="name2"').appendTo("#list_name"); 34 $('<input type="text" id="name3"').appendTo("#list_name"); 35 $('<input type="text" id="name4"').appendTo("#list_name"); 36 $('<input type="text" id="name5"').appendTo("#list_name"); 37 break; 38 } 39});
html
1<!DOCTYPE HTML> 2<html> 3<head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <link rel="stylesheet" href="components/loader.css"> 8 <link rel="stylesheet" href="css/style.css"> 9 <link href="css/bootstrap.min.css" rel="stylesheet"> 10 <script src="components/loader.js"></script> 11 <script src="js/ncmb-2.0.0.min.js"></script> 12 <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script> 13 <script src="js/bootstrap.min.js"></script> 14 <script> 15</head> 16<body> 17<form> 18 <select id="select_number"></select> 19 <li id="list_name"> 20 <input type="text" id="name" /> 21 </li> 22</form> 23</body> 24</html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/15 04:05
退会済みユーザー
2016/01/15 04:05
退会済みユーザー
2016/01/15 04:07