使用JavaScript根据表中选定的月份打印月份的逻辑

时间:2018-06-25 20:04:02

标签: javascript jquery html date javascript-events

我正在获取登录时间和singout时间,但是两者同时意味着当我单击singin退出时间时,当我单击singin退出时间时,登录时间变为空,这是因为使用了两个不同的函数onSigninClick()和onSignoutClick()用于不同的按钮。我该如何在不丢失其他值的情况下打印全部时间,也要打印总小时数,请同时提供JAVASCRIPT中的代码帮助我

<!DOCTYPE html>
<html>

 <head>

  <title>Employee Attendance</title>
 </head>
<body background="b.jpg">
 <center>
  <div align="right">
   <button onclick="onSigninClick()">Signin</button>
   <button onClick="onSignoutClick()">Signout</button>
</div>
 Month:
  <select name="month" id="month" onchange="getMonth(this.value)">
   <option value="">Select Month</option>
   <option value="0">January</option>
   <option value="1">February</option>
   <option value="2">March</option>
   <option value="3">April</option>
   <option value="4">May</option>
   <option value="5">June</option>
   <option value="6">July</option>
   <option value="7">August</option>
   <option value="8">September</option>
   <option value="9">October</option>
   <option value="10">Novermber</option>
   <option value="11">December</option>
  </select>
<table id="monData" border="" cellpadding="15">
 <tr>
  <th>Date</th>
  <th>Sign In</th>
  <th>Sign Out</th>
  <th>Total Hours</th>
 </tr>
</table>
</center>
<script type="text/javascript">
 function onSigninClick(){

     var now = new Date();
 var time = now.getHours() + ":" + now.getMinutes();
 document.getElementById("month").value= now.getMonth()
 getMonth(now.getMonth())
 document.getElementById("signin" + now.getDate()).innerHTML = time;

 }
 function onSignoutClick(){
     var now = new Date();
     var time = now.getHours() + ":" + now.getMinutes();
     document.getElementById("month").value= now.getMonth()
     getMonth(now.getMonth())
     document.getElementById("signout" + now.getDate()).innerHTML = time;
 }

 function get_days_in_month(month,year) {
  return new Date(year, month, 0).getDate();
 }

function getMonth(selected_month){  
 var current_year = new Date().getFullYear()
 var selected_month = (parseInt(selected_month)+1)
 if(month=="")
  alert("Please select Month");
 else{
  var finTab="<tr><th>Date</th><th>Sign In</th><th>Sign Out</th><th>Total Hours</th></tr>";
  var days_in_month = get_days_in_month(selected_month,current_year);
  //alert(days_in_month); 
  for(i =1;i<=days_in_month;i++){
    finTab = finTab + "<tr>";
    finTab = finTab + "<td>"+i+"/" + selected_month   + "/" + current_year + "</td><td id=signin" + i + "></td><td id=signout" + i + "></td><td></td>";
    finTab = finTab + "<tr>";
  } 
  document.getElementById("monData").innerHTML = finTab;
 }  
}    
</script>     
</body>
</html>

2 个答案:

答案 0 :(得分:0)

已经对选项值进行了一些更改,而不是每个月中的天数都刚刚分配了从0到11开始的month值。并且使用了js datetime 对象new Date()使用getFullyear() getDate()获取当前的年份,日期和时间。

function get_days_in_month(month,year),您可以调用此函数,该函数将返回该特定月份的总天数,以2 args作为月份和年份。

单击“登录”按钮将调用onSigninClick()。如果用户选择了其他月份并调用了getMonth(),该月份值将被设置为当前月份,它将呈现当前月份的表。此外,还需要用HH:MM:SS格式的值单击“登录”按钮时,为当前时间分配一个var时间。

finTab = finTab + "<td>"+i+"/" + selected_month   + "/" + current_year + "</td><td id=signin_" + i + "></td><td></td><td></td>"

该值将使用我在代码中分配的列ID打印到特定的日期行。

<!DOCTYPE html>
<html>
 <head>
  <title>Employee Attendance</title>
 </head>
<body>
 <center>
  <div align="right">
   <button onclick="onSigninClick()">Sign In</button>
   <button>Sign out</button>
</div>
 Month: 
  <select name="month" id="month" onchange="getMonth(this.value)">
   <option value="">Select Month</option>
   <option value="0">January</option>
   <option value="1">February</option>
   <option value="2">March</option>
   <option value="3">April</option>
   <option value="4">May</option>
   <option value="5">June</option>
   <option value="6">July</option>
   <option value="7">August</option>
   <option value="8">September</option>
   <option value="9">October</option>
   <option value="10">Novermber</option>
   <option value="11">December</option>
  </select>
<table id="monData" border="" cellpadding="15">
 <tr>
  <th>Date</th>
  <th>Sign In</th>
  <th>Sign Out</th>
  <th>Total Hours</th>
 </tr>
</table>
</center>
<script type="text/javascript">

//  Will be called on Click of Signin Button
 function onSigninClick(){
 var now = new Date(); //  javascript datetime object 
 var time = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();  // Time in HH:MM:SS format 
 document.getElementById("month").value= now.getMonth()  // Set the value of Select Tag
 getMonth(now.getMonth())  // Update the table based on current month
 document.getElementById("signin_" + now.getDate()).innerHTML = time  // print the signin date on respective date
}

// function to get total days in a particular month
 function get_days_in_month(month,year) {
  return new Date(year, month, 0).getDate();
 };

// function to render the month table
function getMonth(selected_month){  
 var current_year = new Date().getFullYear() // current year 
 var selected_month = (parseInt(selected_month)+1)  // selected month 

 if(!month)  // if month not selected alert user
  alert("Please select Month");
 else{
  var finTab="<tr><th>Date</th><th>Sign In</th><th>Sign Out</th><th>Total Hours</th></tr>";
  var days_in_month = get_days_in_month(selected_month,current_year)
  for(i =1;i<=days_in_month;i++){
    finTab = finTab + "<tr>";
    finTab = finTab + "<td>"+i+"/" + selected_month   + "/" + current_year + "</td><td id=signin_" + i + "></td><td></td><td></td>";
    finTab = finTab + "<tr>";
  } 
  document.getElementById("monData").innerHTML = finTab;  // will push the string in the element.
 }  
}    
</script>     
</body>
</html>

答案 1 :(得分:0)

正如拉维所说,月份值最好是月份数,而不是月份中的天数。另外,如果要有多个提交按钮,最好将控件放在表单中,并给按钮起一个名称,以便您知道单击了哪个按钮。

我将在行中使用tbody,这样您就不必每次都重新生成标头。您甚至可以将新行中的单元格数量设置为标题行中的单元格数量,这样就可以调整单元格的数量,而无需调整功能。您还可以在日期标题单元格上放置一个类,以便知道将日期放入哪一列。

我在标题行中使用了thead,但是您可以使用另一个tbody(一个表可以有多个tbody)。

例如

// Helper to format a date as DD/MM/YYYY
function formatDate(d) {
  var z = x => ('0'+x).slice(-2);
  return z(d.getDate()) + '/' + z(d.getMonth()+1) + '/' + d.getFullYear();
}

function getMonth(month) {
  // Get the monData tbody and clear the rows
  var tbody = document.getElementById('monData');
  while (tbody.rows.length) {
    tbody.removeChild(tbody.firstChild);
  }

  // Get the header row, number of cells and date cell index
  // The default date cell is 0 (the first one)
  var headRow = tbody.parentNode.rows[0];
  var cellCount = headRow.cells.length;
  var dateIndex = headRow.querySelector('.dateCell').cellIndex || 0;
  
  // If selected first option, stop here
  if (month == 0) return;
  
  // Create a date for the start of the selected month in the current year
  var date = new Date();
  month -= 1;
  date.setMonth(month, 1);

  // Create a document fragment with the required tr and tds
  var frag = document.createDocumentFragment();
  var oRow = frag.appendChild(document.createElement('tr'));

  for (var i=0; i<cellCount; i++) {
    oRow.appendChild(document.createElement('td'));
  }

  // Clone the row once for each day of the month, putting a 
  // formatted date in the first cell
  while (date.getMonth() == month) {
    var row = oRow.cloneNode(true);
    row.cells[dateIndex].textContent = formatDate(date);
    frag.appendChild(row);
    date.setDate(date.getDate()+1);
  }
  
  // Append the fragment of rows to the tbody
  tbody.appendChild(frag);
}
table {
  border-left: 1px solid #dddddd;
  border-top : 1px solid #dddddd;
  border-collapse: collapse;
}
th, td {
  border-right: 1px solid #dddddd;
  border-bottom : 1px solid #dddddd;
  width: 6em;
}
<form>
  <div align="right">
    <input type="submit" value="Sign In" name="signIn">
    <input type="submit" value="Sign Out" name="signOut">
  </div>
  Month:<select name="month" id="month" onchange="getMonth(this.value)">
    <option value="0">Select Month</option>
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
  </select>
</form>
<table>
  <head>
    <tr><th class="dateCell">Date</th><th>Sign In</th><th>Sign Out</th><th>Total Hours</th></tr>
  </head>
  <tbody id="monData">
  </tbody>
</table>