selectで値を変化させたとき、onchangeを用いて以下2つの値
・status
・staff
をinput type = "hidden"で取ってきたいのですが、
Cannot read property 'appendChild' of undefined
と出てきます。。。hiddenを取ってくるにはどうしたらいいのでしょうか。助力をお願いいたします。以下コードを載せさせていただきます。
またjavascriptについてはhtmlのform記載箇所より下に位置しております。
html
1 <form name="form" id="form"> 2 <fieldset> 3 <p>必要項目を入力して下さい。</p> 4 <p> 5 <label for="name">名</label> 6 <select name="name" id="sname" onChange="Search()"> 7 <option value=""></option> 8 <?php foreach ($Info as $key => $value) {?> 9 <option value="<?php echo $value['id']?>"><?php echo $value['user_name'] ?></option> 10 <?php } ?> 11 </select> 12 </p> 13 </fieldset> 14 </form>
javascript
1<script type="text/javascript"> 2function Search(){ 3 var element = document.getElementById( "name" ); 4 var tmp = element.value; 5 var optionLen = document.form.name.options.length - 1; 6 Info = JSON.parse('<?php echo $InfoJs ?>'); 7 for (var i = 0; i < optionLen; i++) { 8 if (studentInfo[i]['id'] === tmp) { 9 var inputStatus = document.createElement('input'); 10 inputStatus.setAttribute('type','hidden'); 11 inputStatus.setAttribute('name','status'); 12 inputStatus.setAttribute('value',Info[i]['status']); 13 14 document.form.appendChild(inputStatus); 15 16 var inputCl = document.createElement('input'); 17 inputCl.setAttribute('type','hidden'); 18 inputCl.setAttribute('name','status'); 19 inputCl.setAttribute('value',Info[i]['staff']); 20 document.form.appendChild(inputCl); 21 } 22 } 23} 24</script>
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。