### やりたいこと
ブラウザ: chrome
下のコードにある候補日:<input type="datetime-local" name="input" id="input-date">
に値を入力し「入力」ボタンをおすと、<div id="possible_dates_list"></div>
の間に入力した値を保持した、<input type="datetime-local">
を生成したいです。
詳しく書くと,OnButtonClick関数のdateElement変数に値がすでに入力された状態で出力したいのです。
javascript
1<!doctype html> 2<html lang="en"> 3<head> 4 <!-- Required meta tags --> 5 <meta charset="utf-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 7 8 <!-- Bootstrap CSS --> 9 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> 10 11 <title>Hello, world!</title> 12</head> 13<body> 14 15<form method="POST" action="/"> 16 @csrf 17 <div> 18 イベント名:<input type="text" name="event_name" size="40"> 19 </div> 20 <div> 21 詳細: <input type="text" name="detail" size="40"> 22 </div> 23 <div> 24 候補日:<input type="datetime-local" name="input" id="input-date"> 25 <input type="button" name="input" onclick="OnButtonClick();" value="入力"> 26 </div> 27 <div id="possible_dates_list"> 28 </div> 29 <input type="submit" id="create-event-button"> 30</form> 31 32<!-- Optional JavaScript --> 33<!-- jQuery first, then Popper.js, then Bootstrap JS --> 34<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> 35<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> 36<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> 37 38<script> 39 function OnButtonClick() { 40 var dateList = document.getElementById('possible_dates_list'); 41 var dateElement = 42 `<div class="input-group mb-3"> 43 <input type="datetime-local" class="form-control" aria-describedby="basic-addon2" name="possible_dates[]"> 44 <div class="input-group-append"> 45 <button class="btn btn-outline-secondary" type="button">☓</button> 46 </div> 47 </div>`; 48 dateList.insertAdjacentHTML('afterbegin', dateElement); 49 var tmp2 = dateList.lastElementChild; 50 var ggg = tmp2.getElementsByClassName("form-control"); 51 tmp2.getElementsByClassName("form-control")[0].value; 52 ggg[0].value = document.getElementById("input-date"); 53 } 54 // 55 function OnButtonDelete() { 56 element.removeChild(child) 57 } 58 59 function setAttributes(el, attrs) { 60 for(var key in attrs) { 61 el.setAttribute(key, attrs[key]); 62 } 63 } 64</script> 65</body> 66</html> 67
問題点
ですが、値を代入することができません。下の警告が出ます。
new Date("2019-03-03T10:10")
や文字列で"2019-03-03T10:10"
を代入しても下のメッセージが表示されて値を入れることができません。
(index):88 The specified value "[object HTMLInputElement]" does not conform to the required format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS".
希望すること
やりたいことを満たしていただければ良いのですが、もし全体の仕組みをもっと簡単なものがあるのでしたら提案いただけたら嬉しいです。
よろしくお願い致します
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/02 06:25