お世話になります。
つたないコードで申し訳ありませんが、
JavaScriptの処理がうまくいかず悩んでおります。
どこをどう直せば良いのか、アドバイス頂けると幸いです。
●何がしたいか:
カレンダーの各ボタンを押下すると、JavaScriptの処理で
セレクトボックスで選んだユーザーの番号が、
<input type="hidden" name="user" id="user1" />
<input type="hidden" name="user2[]" id="user2[]" />
<input type="hidden" name="user3[]" id="user3[]" />
のvalueにセットされるようにしたい
<?php $year = isset($_POST['year']) ? intval($_POST['year']) : date('Y'); $month = isset($_POST['month']) ? intval($_POST['month']) : date('m'); $day = mktime(0, 0, 0, $month, 1, $year); $first = date('w', $day); $total = date('t', $day); $week = ceil($total / 7); if (($total % 7 > 7 - $first) || ($total % 7 == 0 && $first != 0)) { $week++; } ?> <!DOCTYPE html> <html lang="ja-JP"> <head> <title>master.php</title> <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" /> <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" /> <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css" /> <script type="text/javascript" charset="UTF-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <!--[if lt IE 9]> <script type="text/javascript" charset="UTF-8" src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script type="text/javascript" charset="UTF-8" src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> <script type="text/javascript" charset="UTF-8" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <script> //<![CDATA[ function getValue() { var selectValue = document.forms["user_select"].elements["user"].value; return selectValue; } function setValue() { var val = getValue(); document.getElementById('user1').value = val; } function setValue2() { var val = getValue(); for(var count = 0; count < document.form2["user2[]"].length; count++) { document.form2["user2[]"][count].value = val; } } function setValue3() { var val = getValue(); for(var count = 0; count < document.form3["user3[]"].length; count++) { document.form3["user3[]"][count].value = val; } } //]]> </script> </head> <body> <table> <tr> <td><label>ユーザー:</label></td> <td> <form name="user_select"> <select id="user" name="user"> <option value="1">佐藤</option> <option value="2">鈴木</option> <option value="3">高橋</option> </select> </form> </td> </tr> </table> <table class="table"> <thead> <tr> <th rowspan="<?= ($week + 2) ?>" class="prev"> <form action="master.php" method="post"> <?php if ($month == 1) { echo "<input type='hidden' name='year' value='" . ($year - 1) ."' />"; echo "<input type='hidden' name='month' value='12' />"; } else { echo "<input type='hidden' name='year' value='" . $year ."' />"; echo "<input type='hidden' name='month' value='" . ($month - 1) ."' />"; } ?> <button type="submit"><i class="fa fa-angle-double-left"></i>前月</button> </form> </th> <th colspan="6"> <form action="detail.php" method="post" name="form" onSubmit="setValue()"> <input type="hidden" name="selectflg" value="1" /> <input type="hidden" name="user" id="user1" /> <input type="hidden" name="year" value="<?= $year ?>" /> <input type="hidden" name="month" value="<?= $month ?>" /> <input type="hidden" name="start_day" value="1" /> <input type="hidden" name="end_day" value="<?= $total ?>" /> <input type="submit" value="<?= $year ?>年<?= $month ?>月" /> </form> </th> <th rowspan="<?= ($week + 2) ?>" class="next"> <form action="master.php" method="post"> <?php if ($month == 12) { echo "<input type='hidden' name='year' value='" . ($year + 1) ."' />"; echo "<input type='hidden' name='month' value='1' />"; } else { echo "<input type='hidden' name='year' value='" . $year ."' />"; echo "<input type='hidden' name='month' value='" . ($month + 1) ."' />"; } ?> <button type="submit">次月<i class="fa fa-angle-double-right"></i></button> </form> </th> </tr> </thead> <tbody> <tr> <th class="week">週</th> <th class="sunday">日</th> <th>月</th> <th>火</th> <th>水</th> <th>木</th> <th>金</th> <th class="saturday">土</th> </tr> <tr> <?php for ($i = 1; $i <= $week * 7; $i++) { if ($i % 7 == 1) { ?> <tr> <td class="week"> <form action="detail.php" method="post" name="form2" onSubmit="setValue2()"> <input type="hidden" name="selectflg" value="2" /> <input type="hidden" name="user2[]" value="user2[]" /> <input type="hidden" name="year" value="<?= $year ?>" /> <input type="hidden" name="month" value="<?= $month ?>" /> <?php if ($i - $first >= 0) { if ($i - $first + 6 >= $total) { echo "<input type='hidden' name='start_day' value='" . ($i - $first) . "' />"; echo "<input type='hidden' name='end_day' value='" . $total . "' />"; } else { echo "<input type='hidden' name='start_day' value='" . ($i - $first) . "' />"; echo "<input type='hidden' name='end_day' value='" . ($i - $first + 6) . "' />"; } } else { echo "<input type='hidden' name='start_day' value='1' />"; echo "<input type='hidden' name='end_day' value='" . ($i - $first + 6) . "' />"; } ?> <button type="submit"><i class="fa fa-caret-right"></i></button> </form> </td> <?php } if (($i -1 < $first) || ($i > $total + $first)) { echo "<td><font color=#c0c0c0>・</font></td>"; } else { if ($i % 7 == 1) { ?> <td class="sunday"> <form action="detail.php" method="post" name="form3" onSubmit="setValue3()"> <input type="hidden" name="selectflg" value="3" /> <input type="hidden" name="user3[]" id="user3[]" /> <input type="hidden" name="year" value="<?= $year ?>" /> <input type="hidden" name="month" value="<?= $month ?>" /> <input type="hidden" name="day" value="<?= ($i - $first) ?>" /> <input type="submit" value="<?= ($i - $first) ?>" /> </form> </td> <?php } elseif ($i % 7 == 0) { ?> <td class="saturday"> <form action="detail.php" method="post" name="form3" onSubmit="setValue3()"> <input type="hidden" name="selectflg" value="3" /> <input type="hidden" name="user3[]" id="user3[]" /> <input type="hidden" name="year" value="<?= $year ?>" /> <input type="hidden" name="month" value="<?= $month ?>" /> <input type="hidden" name="day" value="<?= ($i - $first) ?>" /> <input type="submit" value="<?= ($i - $first) ?>" /> </form> </td> <?php } else { ?> <td> <form action="detail.php" method="post" name="form3"" onSubmit="setValue3()"> <input type="hidden" name="selectflg" value="3" /> <input type="hidden" name="user3[]" id="user3[]" /> <input type="hidden" name="year" value="<?= $year ?>" /> <input type="hidden" name="month" value="<?= $month ?>" /> <input type="hidden" name="day" value="<?= ($i - $first) ?>" /> <input type="submit" value="<?= ($i - $first) ?>" /> </form> </td> <?php } } } if ($i % 7 == 0) { echo "</tr>"; } } ?> </tr> </tbody> </table> </body> </html>
どうぞよろしくお願い致します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。