Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>FFSE1702006 - JavaScript Assignment 3</title>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
- <style type="text/css">
- span {
- width: 150px;
- margin-left: 20px;
- float: left;
- }
- span.invalid {
- float: none;
- color: #d05028;
- }
- p {
- margin: 5px 0px;
- }
- #option_show button {
- margin-top: 10px;
- padding: 2px 20px;
- }
- #check {
- margin-left: 170px;
- }
- #test {
- margin-left: 25px;
- }
- select {
- width: 150px;
- }
- #title {
- margin-top: 80px;
- }
- table {
- border-collapse: collapse;
- text-align: center;
- margin: 40px 0px;
- }
- table, td {
- padding: 0.2em 0.5em;
- border: 1px solid black;
- }
- tr:first-child {
- background-color: #d6d6d6;
- }
- tr td:nth-child(n+6) {
- width: 78px;
- }
- tr:nth-child(n+2) td:last-child {
- color: red;
- }
- #tinhtb {
- margin-left: 30px;
- }
- #danhgiahs {
- margin-left: 33%;
- }
- </style>
- </head>
- <body>
- <select onchange="form_input()">
- <option selected disabled hidden>Chọn số lượng</option>
- <option>1</option>
- <option>2</option>
- <option>3</option>
- <option>4</option>
- </select>
- <div id="option_show" style="display: none">
- <p id=title><b>Form nhập liệu:</b></p>
- <br>
- <form id=form style="display: none">
- <p><span>Họ tên</span>
- <input class="name" type="text">
- <span class="invalid check_name">Họ tên bắt buộc nhập</span>
- </p>
- <p><span>Lớp</span>
- <input class="classs" type="text">
- <span class="invalid check_classs">Lớp bắt buộc nhập</span>
- </p>
- <p><span>Email</span>
- <input class="email" type="text">
- <span class="invalid check_email">Email phải nhập và nhập đúng định dạng</span>
- </p>
- <p><span>Số điện thoại</span>
- <input class="phone" type="text">
- <span class="invalid check_phone">Số điện thoại phải nhập và nhập đúng định dạng (xxx xxx xxxx)</span>
- </p>
- <p><span>Điểm Văn</span>
- <input class="p1" type="number">
- <span class="invalid check_p1">Điểm Văn phải nhập và phải là số từ 0 - 10</span>
- </p>
- <p><span>Điểm Đạo Đức</span>
- <input class="p2" type="number">
- <span class="invalid check_p2">Điểm Đạo Đức phải nhập và phải là số từ 0 - 10</span>
- </p>
- <p><span>Điểm Lập Trình</span>
- <input class="p3" type="number">
- <span class="invalid check_p3">Điểm Lập Trình phải nhập và phải là số từ 0 - 10</span>
- </p>
- </form>
- <div id=show>
- </div>
- <button type="submit" onclick="check()" id="check">Nhập</button>
- <button type="submit" onclick="test()" id="test">Test</button>
- </div>
- <div id="table_show" style="display: none">
- <table>
- <tr>
- <td>STT</td>
- <td>Họ Tên</td>
- <td>Lớp</td>
- <td>Email</td>
- <td>SDT</td>
- <td>Văn</td>
- <td>Đạo Đức</td>
- <td>Lập Trình</td>
- <td>Trung Bình</td>
- </tr>
- </table>
- <button type="submit" onclick="danhgiahs()" id="danhgiahs">Đánh giá HS</button>
- <button type="submit" onclick="tinhtb()" id="tinhtb">Tính TB</button>
- </div>
- <script type="text/javascript">
- form_html = $("#form").html();
- i = 0;
- stt_print = 0;
- sv = [];
- function form_input() {
- $("#option_show").show();
- text = "";
- for (var i = 1; i <= $("option:selected").text(); i++) {
- if (i === 1) {
- $("#form").show();
- } else {
- text += "<hr align='left' width='350px'><form>" + form_html + "</form>";
- }
- }
- $("#show").html(text);
- $(".invalid").hide();
- $("form").each(function() {
- this.reset();
- });
- }
- function check_valid() {
- $("form").each(function() {
- // Check name
- name = $(this).find(".name").val();
- if (name === "") {
- $(this).find(".check_name").show();
- } else {
- $(this).find(".check_name").hide();
- }
- // Check classs
- classs = $(this).find(".classs").val();
- if (classs === "") {
- $(this).find(".check_classs").show();
- } else {
- $(this).find(".check_classs").hide();
- }
- // Check email
- email = $(this).find(".email").val();
- if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
- $(this).find(".check_email").hide();
- } else {
- $(this).find(".check_email").show();
- }
- // Check phone
- phone = $(this).find(".phone").val();
- if (/^\d{3} \d{3} \d{4}$/.test(phone)) {
- $(this).find(".check_phone").hide();
- } else {
- $(this).find(".check_phone").show();
- }
- // Check p1
- p1 = $(this).find(".p1").val();
- if (isNaN(p1) || p1 < 0 || p1 > 10 || p1 === "") {
- $(this).find(".check_p1").show();
- } else {
- $(this).find(".check_p1").hide();
- }
- // Check p2
- p2 = $(this).find(".p2").val();
- if (isNaN(p2) || p2 < 0 || p2 > 10 || p2 === "") {
- $(this).find(".check_p2").show();
- } else {
- $(this).find(".check_p2").hide();
- }
- // Check p3
- p3 = $(this).find(".p3").val();
- if (isNaN(p3) || p3 < 0 || p3 > 10 || p3 === "") {
- $(this).find(".check_p3").show();
- } else {
- $(this).find(".check_p3").hide();
- }
- });
- }
- function check() {
- check_valid();
- if ($(".invalid").is(":visible")) {} else {
- $("form").each(function() {
- name = $(this).find(".name").val();
- classs = $(this).find(".classs").val();
- email = $(this).find(".email").val();
- phone = $(this).find(".phone").val();
- p1 = $(this).find(".p1").val();
- p2 = $(this).find(".p2").val();
- p3 = $(this).find(".p3").val();
- tb = ((+p1 + +p2 + +p3) / 3).toFixed(1);
- stt = i + 1;
- sv[i] = new data(stt, name, classs, email, phone, p1, p2, p3, tb);
- i++;
- this.reset();
- });
- table();
- }
- }
- function data(stt, name, classs, email, phone, p1, p2, p3, tb) {
- this.stt = stt;
- this.name = name;
- this.classs = classs;
- this.email = email;
- this.phone = phone;
- this.p1 = p1;
- this.p2 = p2;
- this.p3 = p3;
- this.tb = tb;
- }
- function table() {
- $("#table_show").show();
- table_html = "";
- for (; stt_print < sv.length; stt_print++) {
- table_html += "<tr>";
- for (x in sv[stt_print]) {
- table_html += "<td>" + sv[stt_print][x];
- }
- }
- $("table").append(table_html);
- $("tr:nth-child(n+2)").each(function() {
- $(this).find("td:last-child").html("?");
- });
- }
- function tinhtb() {
- var i = 0;
- $("tr:nth-child(n+2)").each(function() {
- $(this).find("td:last-child").html(sv[i].tb);
- i++;
- });
- }
- function danhgiahs() {
- $("tr td:nth-child(n+6)").each(function() {
- if ($(this).text() >= 8) {
- $(this).css({ "color": "red", "font-weight": "bold" });
- } else {
- if ($(this).text() <= 4) {
- $(this).css({ "color": "blue", "font-weight": "bold" });
- }
- }
- });
- $("tr:nth-child(n+2) td:last-child").css({ "color": "red", "font-weight": "normal" });
- }
- function test() {
- sv_test = [];
- var i = 0;
- $("form").each(function() {
- $(this).find(".name").val(sv_test[i].name);
- $(this).find(".classs").val(sv_test[i].classs);
- $(this).find(".email").val(sv_test[i].email);
- $(this).find(".phone").val(sv_test[i].phone);
- $(this).find(".p1").val(sv_test[i].p1);
- $(this).find(".p2").val(sv_test[i].p2);
- $(this).find(".p3").val(sv_test[i].p3);
- i++;
- });
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement