javascript 添加行,删除行,datepicker获取当前日期和上一个月日期并设置格式,笔记

$(function () {

getdatepicker();
today();
getPreMonth();
getdatetimepicker();

});
function today(){
$("input[name='applydate']").datepicker('setDate',new Date());
$("input[name='applydate2']").datepicker('setDate',new Date());

}
$(document).on('click','#addTable',addTr);
$(document).on('click','#deleteTable',deleteTr);


function getdatepicker()
{
$("input[name='applydate']").datepicker({
clearBtn : true,
language : "ja",
autoclose : true
});
$("input[name='applydate1']").datepicker({
clearBtn : true,
language : "ja",
autoclose : true
});
$("input[name='applydate2']").datepicker({
clearBtn : true,
language : "ja",
autoclose : true
});
}

function getdatetimepicker()
{
$("#applyDateTimeFrom").datetimepicker({
clearBtn : true,
language : "ja",
autoclose : true
});
$("#applyDateTimeTo").datetimepicker({
clearBtn : true,
language : "ja",
autoclose : true
});
}
//delete now Row
function delRow(obj){
var row = obj.parentNode.parentNode;
var tb = row.parentNode;
var rowIndex = row.rowIndex-1;
tb.deleteRow(rowIndex);
}

function deleteTr(){

var i=$('#ApplaycountTable').find('tr').size();
if(i!=2)
{
$("#ApplaycountTable tr").eq(i-1).remove();
}
}

function addTr(){
var tr_len=$('#ApplaycountTable').find('tr').size();
var i=tr_len-1;

var tr="<tr>";
tr=tr+"<td class='col-lg-2' id='applydatetb"+i+"'>";
tr=tr+" <div class='input-group date' id='datepicker"+i+"'>";
tr=tr+" <input type='text' class='apply-form-control input-sm col-lg-1' id='clickDateFrom"+i+"' name='datepicker'>";
tr=tr+" <span class='input-group-addon'><i class='glyphicon glyphicon-th'></i></span>";
tr=tr+" </div>";
tr=tr+"</td>";
tr=tr+"<td class='col-lg-2' id='applykbtb"+i+"'>";
tr=tr+"<form role='form'>";
tr=tr+" <div class='form-group'>";
tr=tr+" <select class='apply-form-control'>";
tr=tr+" <option value='11'>事假</option>";
tr=tr+" <option value='12'>病假</option>";
tr=tr+" <option value='13'>加班</option>";
tr=tr+" <option value='2'>年假</option>";
tr=tr+" <option value='3'>婚假</option>";
tr=tr+" <option value='4'>产假</option>";
tr=tr+" <option value='5'>丧假</option>";
tr=tr+" <option value='7'>出差</option>";
tr=tr+" <option value='8'>忘打卡</option>";
tr=tr+" <option value='9'>迟到</option>";
tr=tr+" </select>";
tr=tr+" </div>";
tr=tr+" </form>";
tr=tr+"</td>";
tr=tr+"<td class='col-lg-2' id='startDateTimetb"+i+"'>";
tr=tr+" <div class='form-group'> ";
tr=tr+" <div class='input-group datetime' id='datetimepicker1"+i+"'>";
tr=tr+" <input type='text' class='apply-form-control input-sm col-lg-1' id='applyDateTimeFrom"+i+"' name='datetime' >";
tr=tr+" <span class='input-group-addon'><i class='glyphicon glyphicon-th'></i></span>";
tr=tr+" </div> ";
tr=tr+" </div> ";
tr=tr+"</td>";
tr=tr+"<td class='col-lg-2' id='EndDateTimetb"+i+"'>";
tr=tr+" <div class='input-group datetime' id='datetimepicker2"+i+"'>";
tr=tr+" <input type='text' class='apply-form-control input-sm col-lg-1' id='applyDateTimeTo"+i+"' name='datetime' >";
tr=tr+" <span class='input-group-addon'><i class='glyphicon glyphicon-th'></i></span>";
tr=tr+" </div>";
tr=tr+"</td>";
tr=tr+"<td class='col-lg-2' id='reasontb"+i+"'>";
tr=tr+" <input type='text' class='apply-form-control input-sm col-lg-1' name='reason' id='reason"+i+"'></input>";
tr=tr+" </td>";
tr=tr+ "<td class='col-lg-2' id='addBtntb"+i+"'>";
tr=tr+ "<span class='glyphicon glyphicon-plus-sign' aria-hidden='true' id='addTable"+i+"' onclick='addTr()'></span>";
tr=tr+"</td>";
tr=tr+" <td class='col-lg-2' id='delBtntb"+i+"'>";
tr=tr+" <span class='glyphicon glyphicon-minus-sign' aria-hidden='true' id='deleteTable"+i+"' onclick='delRow(this)'></span>";
tr=tr+"</td>";
tr=tr+"</tr>";
$("#ApplaycountTable").append(tr);  

$("#clickDateFrom"+i).datepicker({
clearBtn : true,
language : "ja",
autoclose : true
});
$("#clickDateFrom"+i).datepicker('setDate',new Date());
$("#applyDateTimeFrom"+i).datetimepicker({
clearBtn : true,
language : "ja",
autoclose : true
});
$("#applyDateTimeTo"+i).datetimepicker({
clearBtn : true,
language : "ja",
autoclose : true
});
  }
//get pre month
function getPreMonth() {
var date=new Date().Format("yyyy-MM-dd");
var arr = date.split('-');
var year = arr[0]; //获取当前日期的年份
var month = arr[1]; //获取当前日期的月份
var day = arr[2]; //获取当前日期的日
var days = new Date(year, month, 0);
days = days.getDate(); //获取当前日期中月的天数
var year2 = year;
var month2 = parseInt(month) - 1;
if (month2 == 0) {//如果是1月份,则取上一年的12月份
year2 = parseInt(year2) - 1;
month2 = 12;
}
var day2 = day;
var days2 = new Date(year2, month2, 0);
days2 = days2.getDate();
if (day2 > days2) {//如果原来日期大于上一月的日期,则取当月的最大日期。比如3月的30日,在2月中没有30
day2 = days2;
}
if (month2 < 10) {
month2 = '0' + month2;//月份填补成2位。
}
var t2 = year2 + '-' + month2 + '-' + day2;
$("input[name='applydate1']").datepicker('setDate',t2);
}

//set Date Format

Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}

posted @ 2017-03-07 10:24  公众号python学习开发  阅读(323)  评论(0编辑  收藏  举报