ในตัวอย่างนี้เราขอนำเสนอ การสร้าง ฟอร์ม HTML ตัวเลือกแบบสอบถามอย่างง่าย ๆ ซึ่งจะใช้ input ที่ มี Type เป็น radio โดยเราจะเพิ่มการแสดง Progress Bar
เปอร์เซ็นต์ การ เลือก ตอบคำถาม ว่าผู้ตอบคำถามได้ตอบคำถามไปแล้วกี่เปอร์เซ็นต์
สามารถนำไปประยุกต์ใช้กับการกรอกแบบฟอร์มในรูปแบบต่าง ๆ ได้
ไปดูตัวอย่างกันเลย Demo Form Progress Bar
มาเริ่มกันเลย
1.สร้างไฟล์ index.html นำเข้า javascript และ Css ที่จำเป็น
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Question Progress</title> <!-- นำเข้า Css Bootstrap --> <link rel="stylesheet" href="css/bootstrap.min.css"> </head> <body> <div class="container"> </div> <!-- นำเข้า ไฟล์ JS --> <script src="js/jquery.min.js"></script> <script src="js/bootstrap.min.js"></script> </body> </html> |
2.เพิ่มเติมไฟล์ index.html เรียกใช้ class progress-bar จาก Bootstrap และ สร้างฟอร์ม Html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Question Progress</title> <link rel="stylesheet" href="css/bootstrap.min.css"> <style> .question-table{ text-align:center; } .question-title{ text-align:left; } </style> </head> <body> <div class="container"> <div class="col-md-12"> <h3>EX Question Progress Bar</h3> <hr> <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="min-width: 4em;width: 0%;"> 0 % </div> </div> <form id="question-form"> <table class="table table-bordered question-table"> <tbody> <tr> <td rowspan="2" width="70">ลำดับ</td> <td rowspan="2" width="280">เนื้อหา</td> <td colspan="5" height="25" width="450">ระดับความพึงพอใจของผู้เรียน</td> </tr> <tr> <td height="25" width="90" >มากที่สุด</td> <td width="90">มาก</td> <td width="90">ปานกลาง</td> <td width="90">น้อย</td> <td width="90">น้อยที่สุด</td> </tr> <tr> <td height="25" >1</td> <td class="question-title"> อธิบายเนื้อหาเข้าใจง่าย</td> <td> <input type="radio" name="question1" onchange="return checkForm('question1')" value="5"> </td> <td> <input type="radio" name="question1" onchange="return checkForm('question1')" value="4"> </td> <td> <input type="radio" name="question1" onchange="return checkForm('question1')" value="3"> </td> <td> <input type="radio" name="question1" onchange="return checkForm('question1')" value="2"> </td> <td> <input type="radio" name="question1" onchange="return checkForm('question1')" value="1"> </td> </tr> <tr> <td height="25" >2</td> <td class="question-title"> สามารถนำเนื้อหาที่ได้รับไปใช้ในชีวิตประจำวันได้</td> <td> <input type="radio" name="question2" onchange="return checkForm('question2')" value="5"> </td> <td> <input type="radio" name="question2" onchange="return checkForm('question2')" value="4"> </td> <td> <input type="radio" name="question2" onchange="return checkForm('question2')" value="3"> </td> <td> <input type="radio" name="question2" onchange="return checkForm('question2')" value="2"> </td> <td> <input type="radio" name="question2" onchange="return checkForm('question2')" value="1"> </td> </tr> <tr> <td height="25" >3</td> <td class="question-title"> เนื้อหาทันสมัยทันกับเหตุการณ์ในปัจจุบัน</td> <td> <input type="radio" name="question3" onchange="return checkForm('question3')" value="5"> </td> <td> <input type="radio" name="question3" onchange="return checkForm('question3')" value="4"> </td> <td> <input type="radio" name="question3" onchange="return checkForm('question3')" value="3"> </td> <td> <input type="radio" name="question3" onchange="return checkForm('question3')" value="2"> </td> <td> <input type="radio" name="question3" onchange="return checkForm('question3')" value="1"> </td> </tr> <tr> <td height="25" >4</td> <td class="question-title"> เอกสารการสอนมีความละเอียดเข้าใจง่าย</td> <td> <input type="radio" name="question4" onchange="return checkForm('question4')" value="5"> </td> <td> <input type="radio" name="question4" onchange="return checkForm('question4')" value="4"> </td> <td> <input type="radio" name="question4" onchange="return checkForm('question4')" value="3"> </td> <td> <input type="radio" name="question4" onchange="return checkForm('question4')" value="2"> </td> <td> <input type="radio" name="question4" onchange="return checkForm('question4')" value="1"> </td> </tr> <tr> <td height="25" >5</td> <td class="question-title"> เนื้อหาการสอนเหมาะสมกับ ความรู้พื้นฐานของผู้เรียน</td> <td> <input type="radio" name="question5" onchange="return checkForm('question5')" value="5"> </td> <td> <input type="radio" name="question5" onchange="return checkForm('question5')" value="4"> </td> <td> <input type="radio" name="question5" onchange="return checkForm('question5')" value="3"> </td> <td> <input type="radio" name="question5" onchange="return checkForm('question5')" value="2"> </td> <td> <input type="radio" name="question5" onchange="return checkForm('question5')" value="1"> </td> </tr> </tbody> </table> <div class="text-center"> <hr> <button id="submit-form" class="btn btn-success">Send Answer</button> </div> </form> <script src="js/jquery.min.js"></script> <script src="js/bootstrap.min.js"></script> </body> </html> |
3.เขียน Script ตรวจสอบการ เลือกข้อคำถาม และ แสดง Progress Bar ใน แบบฟอร์ม
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<script> $(function(){ $("#submit-form").click(function(){ alert($( "form" ).serialize()); return false; }); }); //กำหนดค่าเริ่มต้น progress bar progress = 0; //ตัวแปร checked สำหรับ เก็บ ข้อที่ได้ตอบไปแล้ว checked = []; //จำนวนข้อทั้งหมด number = 5; //ฟังชั่นตรวจสอบการเลือกคำถามในแบบฟอร์ม function checkForm(qname){ //ตรวจสอบว่ามีการเลือกไปแล้วหรือยัง if(!isChecked(qname)){ progress ++; checked.push(qname); } //แสดง progress bar เป็น แบบ เปอร์เซ้น show_progress = (progress * 100) / number; $(".progress-bar").attr('style','width:'+show_progress+'%').text(parseInt(show_progress)+' %'); } //ฟังชั่น ตรวจสอบ ว่า มีการ เลือกคำตอบไปแล้วหรือยัง function isChecked(qname){ for(i = 0; i<=checked.length;i++){ if(checked[i] == qname){ //คืนค่าจริง หาก มีการเลือกข้อนี้ไปแล้ว return true; } } //คืนค่าเท็จ กรณียังไม่มีการเลือก return false; } </script> |