自己总结的常用Jquery代码–前端程序员必藏 – 码农在线

自己总结的常用Jquery代码–前端程序员必藏

获取一组radio被选中项的值var item = $(‘input[@name=items][@checked]‘).val();获取select被选中项的文本var item = $(“select[@name=items] option[@selected]“).text();select下拉框的第二个元素为当前选中值$(‘#select_id’)[0].selectedIndex = 1;radio单选组的第二个元素为当前选中值$(‘input[@name=items]‘).get(1).checked = true;获取值:文本框,文本区域:$(“#txt”).attr(“value”);多选框checkbox:$(“#checkbox_id”).attr(“value”);单选组radio: $(“input[@type=radio][@checked]“).val();下拉框select: $(‘#sel’).val();控制表单元素:文本框,文本区域:$(“#txt”).attr(“value”,”);//清空内容$(“#txt”).attr(“value”,’11′);//填充内容多选框checkbox: $(“#chk1″).attr(“checked”,”);//不打勾$(“#chk2″).attr(“checked”,true);//打勾if($(“#chk1″).attr(‘checked’)==undefined) //判断是否已经打勾单选组radio: $(“input[@type=radio]“).attr(“checked”,’2′);//设置value=2的项目为当前选中项下拉框select: $(“#sel”).attr(“value”,’-sel3′);//设置value=-sel3的项目为当前选中项$(“<option value=’1′>1111</option><option value=’2′>2222</option>”).appendTo(“#sel”)//添加下拉框的option$(“#sel”).empty();//清空下拉框jquery 邮箱 手机号码 用户名验证正则表达式本文章从网上收藏了一个利用jquery来验证邮箱地址合法性,手机号码验证包括所有手机号码都可以验证哦,最的验证用户名只允许是字符数字下划线以及@符号。var name = $(‘#regemail’).val();var search_str = /^[w-.]+@[w-.]+(.w+)+$/;if(!search_str.test(name)){alert(“邮箱不合法!”);$(“#regemail”).val(”);$(“#regemail”).focus();return false;}setInterval(fromCity,500);function fromCity(){//取车城市$(‘#fromCityCode’).change(function(){//alert($(this).children(‘option:selected’).val());var p1=$(this).children(‘option:selected’).val();//这就是selected的值$(‘#toCityCode’).val(p1);$(‘#toCityCode’).change();});//取车门店$(‘#fromLocationId’).change(function(){var p2=$(this).children(‘option:selected’).val();$(‘#toLocationId’).val(p2);});}var mob=$.trim($(“#mobile”).val());if($.trim($(“#mobile”).val())==”"){alert(“手机号码不能为空!”);return false;}if($.trim($(“#mobile”).val())!=”"){var reg = /^(?:13d|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|4|5|6|7|8|9])-?d{5}(d{3}|*{3})$/;if(!reg.test($.trim($(‘#mobile’).val()))){alert(“手机号码格式不对!”);return false;}}1。 // 切换效果和添加删除CSS样式function changeStyle(tabId) {if(tabId==”chengke”){showDiv(‘driver’);hidenDiv(‘passenger’);$(“#chengke”).addClass(‘bltabs_on’);$(“#siji”).removeClass(‘bltabs_on’);}else if(tabId==”siji”){showDiv(‘passenger’);hidenDiv(‘driver’);$(“#siji”).addClass(‘bltabs_on’);$(“#chengke”).removeClass(‘bltabs_on’);}}2. // 光标进入输入框清空文本框内容function inputIn(tId){var this_val = $.trim($(“#”+tId).val());if(this_val == ” || this_val == “出发地,行政,交通,商圈” || this_val == “目的地,行政,交通,商圈”){$(“#”+tId).val(”);}}// 光标离开输入框function inputOut(tId){var this_val = $.trim($(“#”+tId).val());if(this_val == ” || this_val == “出发地,行政,交通,商圈”){$(“#”+tId).val(‘出发地,行政,交通,商圈’);}else if(this_val == “目的地,行政,交通,商圈”){$(“#”+tId).val(‘目的地,行政,交通,商圈’);}}// 根据复选框设置文本框是否可写function setTextByBox(bId){var box = $(“#”+bId);if(bId==”lineOne”){if(box.attr(‘checked’)==true){$(“#singlePrice”).attr(“readonly”,”);}else{$(“#singlePrice”).attr(“readonly”,’true’);}}if(bId==”lineReturn”){if(box.attr(‘checked’)==true){$(“#doublePrice”).attr(“readonly”,”);}else{$(“#doublePrice”).attr(“readonly”,’true’);}}if(bId==”lineMonth”){if(box.attr(‘checked’)==true){$(“#monthPrice”).attr(“readonly”,”);}else{$(“#monthPrice”).attr(“readonly”,’true’);}}}javascript转换日期字符串为Date对象把一个日期字符串如“2007-2-28 10:18:30”转换为Date对象:1:1var strArray=str.split(” “);2var strDate=strArray[0].split(“-”);3var strTime=strArray[1].split(“:”);4var a=new Date(strDate[0],(strDate[1]-parseInt(1)),strDate[2],strTime[0],strTime[1],strTime[2])2:1var s = “2005-12-15 09:41:30″;2var d = new Date(Date.parse(s.replace(/-/g, “/”)));只能输入数字和小数点的文本框:<input onkeyup=”value=value.replace(/[^\d\.]/g,”)”>jquery获取复选框的值<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”><html><head><mce:style><!––></mce:style><style mce_bogus=”1″></style><title>JS获取复选框被选中的值</title></head><body><input type=”checkbox” name=”test” value=”0″ />0<input type=”checkbox” name=”test” value=”1″ />1<input type=”checkbox” name=”test” value=”2″ />2<input type=”checkbox” name=”test” value=”3″ />3<input type=”checkbox” name=”test” value=”4″ />4<input type=”checkbox” name=”test” value=”5″ />5<input type=”checkbox” name=”test” value=”6″ />6<input type=”checkbox” name=”test” value=”7″ />7<input type=”button” onclick=”chk()” value=”提 交” /></body></html<mce:script src=”jquery.js” mce_src=”jquery.js”></mce:script><!–这是载入jquery.js文件,如果不使用jquery可以去掉–><mce:script type=”text/javascript”><!–function chk(){var obj=document.getElementsByName(‘test’); //选择所有name=”‘test’”的对象,返回数组//取到对象数组后,我们来循环检测它是不是被选中var s=”;for(var i=0; i<obj.length; i++){if(obj[i].checked) s+=obj[i].value+’,'; //如果选中,将value添加到变量s中}//那么现在来检测s的值就知道选中的复选框的值了alert(s==”?’你还没有选择任何内容!’:s);}function jqchk(){ //jquery获取复选框值var chk_value =[];$(‘input[name="test"]:checked’).each(function(){chk_value.push($(this).val());});alert(chk_value.length==0 ?’你还没有选择任何内容!’:chk_value);}// –></mce:script>对checkbox的其他几个操作1. 全选2. 取消全选3. 选中所有奇数4. 反选5. 获得选中的所有值js代码$(“document”).ready(function(){$(“#btn1″).click(function(){$(“[name='checkbox']“).attr(“checked”,’true’);//全选})$(“#btn2″).click(function(){$(“[name='checkbox']“).removeAttr(“checked”);//取消全选})$(“#btn3″).click(function(){$(“[name='checkbox']:even”).attr(“checked”,’true’);//选中所有奇数})$(“#btn4″).click(function(){$(“[name='checkbox']“).each(function(){//反选if($(this).attr(“checked”)){$(this).removeAttr(“checked”);}else{$(this).attr(“checked”,’true’);}})})$(“#btn5″).click(function(){//输出选中的值var str=”";$(“[name='checkbox'][checked]“).each(function(){str+=$(this).val()+”/r/n”;//alert($(this).val());})alert(str);})})<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”><html xmlns=”http://www.w3.org/1999/xhtml”><head><meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /><title>louis-blog >> jQuery 对checkbox的操作</title><mce:script type=’text/javascript’ src=”http://leotheme.cn/wp-includes/js/jquery/jquery.js” mce_src=”http://leotheme.cn/wp-includes/js/jquery/jquery.js”></mce:script><SCRIPT LANGUAGE=”JavaScript”><!–$(“document”).ready(function(){$(“#btn1″).click(function(){$(“[name='checkbox']“).attr(“checked”,’true’);//全选})$(“#btn2″).click(function(){$(“[name='checkbox']“).removeAttr(“checked”);//取消全选})$(“#btn3″).click(function(){$(“[name='checkbox']:even”).attr(“checked”,’true’);//选中所有奇数})$(“#btn4″).click(function(){$(“[name='checkbox']“).each(function(){//反选if($(this).attr(“checked”)){$(this).removeAttr(“checked”);}else{$(this).attr(“checked”,’true’);}})})$(“#btn5″).click(function(){//输出选中的值var str=”";$(“[name='checkbox'][checked]“).each(function(){str+=$(this).val()+”/r/n”;//alert($(this).val());})alert(str);})})–></SCRIPT></HEAD><body style=”text-align:center;margin: 0 auto;font-size: 12px;” mce_style=”text-align:center;margin: 0 auto;font-size: 12px;”><div style=”border: 1px solid #999; width: 500px; padding: 15px; background: #eee; margin-top: 150px;”><form name=”form1″ method=”post” action=”"><input type=”button” id=”btn1″ value=”全选”><input type=”button” id=”btn2″ value=”取消全选”><input type=”button” id=”btn3″ value=”选中所有奇数”><input type=”button” id=”btn4″ value=”反选”><input type=”button” id=”btn5″ value=”获得选中的所有值”><br /><br /><input type=”checkbox” name=”checkbox” value=”checkbox1″>checkbox1<input type=”checkbox” name=”checkbox” value=”checkbox2″>checkbox2<input type=”checkbox” name=”checkbox” value=”checkbox3″>checkbox3<input type=”checkbox” name=”checkbox” value=”checkbox4″>checkbox4<input type=”checkbox” name=”checkbox” value=”checkbox5″>checkbox5<input type=”checkbox” name=”checkbox” value=”checkbox6″>checkbox6</form></div></body></HTML>$(“document”).ready(function(){/*** 线路发布第一步,周期选择* alpha at 2012-4-13 11:03:59*/var work = $(“#work”);var all = $(“#all”);// 全选all.click(function(){var btnAllText = $(“#all”).text();if(btnAllText==”全选”){$(“#all”).text(“全取消”);for(var i=1;i<8;i++){$(“#work”+i).attr(“checked”,’true’);}}else if(btnAllText==”全取消”){$(“#all”).text(“全选”);for(var i=1;i<8;i++){$(“#work”+i).attr(“checked”,”);}}})// 工作日work.click(function(){var btnWorkText = $(“#work”).text();if($(“#work6″).attr(‘checked’)==true || $(“#work7″).attr(‘checked’)==true){$(“#work6″).attr(“checked”,”);$(“#work7″).attr(“checked”,”);return;}if(btnWorkText==”工作日拼车”){$(“#work”).text(“工作日不拼车”);for(var i=1;i<6;i++){$(“#work”+i).attr(“checked”,’true’);/*if($(“#work”+i).attr(‘checked’)==false){$(“#work”+i).attr(“checked”,’true’);}else{$(“#work”+i).attr(“checked”,”);}*/}}else if(btnWorkText==”工作日不拼车”){$(“#work”).text(“工作日拼车”);for(var i=1;i<6;i++){$(“#work”+i).attr(“checked”,”);}}})// 点击后增加一个日期输入框,偶发拼车允许多个出发时间。$(“#addDateText”).click(function(){// alert($(“#lineDateM”).attr(“name”));var name = $(“#lineDateM”).attr(“name”);if(name==undefined){name = $(“#lineDate”).attr(“name”);}//生成 0 到10 随机数 Mapth.random产生 0到1的随机数 然后乘以10var maxNumber = 1000;var randomNumber = Math.round(maxNumber * Math.random());var s = “<div id=’div”+randomNumber+”‘><input class=’set_pc_wftime’ type=’text’ name=’”+name+”‘ onclick=\”WdatePicker({dateFmt:’yyyy-MM-dd’, skin:’whyGreen’})\”/>”;s += “<input type=’button’ value=’删除’ onclick=’del(” + randomNumber + “)’/></div>”;var $mm = $(s);var $parent = $(“#addText”);$parent.append($mm);})// 点击后增加一个输入框,拼车允许多个标签$(“#addLabel”).click(function(){//生成 0 到10 随机数 Mapth.random产生 0到1的随机数 然后乘以10var maxNumber = 1000;var randomNumber = Math.round(maxNumber * Math.random());var s = “<div id=’div”+randomNumber+”‘><input type=’text’ name=’lineLableDescribe’/>”;s += “<input type=’button’ value=’删除’ onclick=’del(” + randomNumber + “)’/></div>”;var $mm = $(s);var $parent = $(“#addTextLabel”);$parent.append($mm);})})// 点击后增加一个日期输入框的删除按钮function del(id) {$(“#div” + id).remove();}// 根据id隐藏divfunction hidenDiv(div) {$(“#” + div).hide();}// 根据id显示divfunction showDiv(div) {$(“#” + div).show();}// 线路发布第一步,修改线路发布时的用户身份function chengeType(id) {if(id==”siji”){$(“#isProvideCar”).val(“1″);}else if(id==”chengke”){$(“#isProvideCar”).val(“2″);}}// 验证汉字,是汉字返回true,否则返回falsefunction isChinese(v){var reg = /^[\u4e00-\u9fa5]+$/i;if (!reg.test(v)){alert(“请输入中文”);return false;}else{return true;}}// 验证数字,是数字返回true,否则返回falsefunction isNumber(v){if(isNaN(v)){alert(“必须是数字”);return false;}return true;}// 验证手机号码function checkMobile(v){var reg = /^(?:13d|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|4|5|6|7|8|9])-?d{5}(d{3}|*{3})$/;if(!reg.test(mobilePhone)) return false;return true;}JQuery获取和设置Select选项获取Select :获取select 选中的 text :$(“#ddlRegType”).find(“option:selected”).text();获取select选中的 value:$(“#ddlRegType “).val();获取select选中的索引:$(“#ddlRegType “).get(0).selectedIndex;设置select:设置select 选中的索引:$(“#ddlRegType “).get(0).selectedIndex=index;//index为索引值设置select 选中的value:$(“#ddlRegType “).attr(“value”,”Normal“);$(“#ddlRegType “).val(“Normal”);$(“#ddlRegType “).get(0).value = value;设置select 选中的text:var count=$(“#ddlRegType option”).length;for(var i=0;i<count;i++){ if($(“#ddlRegType “).get(0).options[i].text == text){$(“#ddlRegType “).get(0).options[i].selected = true;break;}}清空 Select:$(“#ddlRegType “).empty();实时监听js改变value,跨浏览器支持<table><tr><td>此处值通过js设置:</td><td><input id=”jsUserName” type=”text” name=”jsUserName” readonly/></td></tr><tr><td>此处输入值:</td><td><input id=”userName” type=”text” name=”userName”/></td></tr></table>Javascript代码$(function(){var jsUserName = “”;if($.browser.msie) // IE浏览器{$(“#userName”).get(0).onpropertychange = setJsUserName;$(“#jsUserName”).get(0).onpropertychange = handle;}else // 其他浏览器{var intervalName; // 定时器句柄$(“#userName”).get(0).addEventListener(“input”,setJsUserName,false);// 获得焦点时,启动定时器$(“#userName”).focus(function(){intervalName = setInterval(handle,1000);});// 失去焦点时,清除定时器$(“#userName”).blur(function(){clearInterval(intervalName);});}// 设置jsUserName input的值function setJsUserName(){$(“#jsUserName”).val($(this).val());}// jsUserName input的值改变时执行的函数function handle(){// IE浏览器此处判断没什么意义,但为了统一,且提取公共代码而这样处理。if($(“#jsUserName”).val() != jsUserName){$(“#toolTip”).remove();$(“#jsUserName”).parent().append(“<span id=’toolTip’>看到这里的信息表明,通过js改变input的值也能响应相应事件:<span style=’color:red;’>” + $(“#jsUserName”).val() + “</span></span>”);jsUserName = $(“#jsUserName”).val();}}});$(“document”).ready(function(){/*** 线路发布第一步,周期选择* alpha at 2012-4-13 11:03:59*/var work = $(“#work”);var all = $(“#all”);// 全选all.click(function(){var btnAllText = $(“#all”).text();if(btnAllText==”全选”){$(“#all”).text(“全取消”);for(var i=1;i<8;i++){$(“#work”+i).attr(“checked”,’true’);}}else if(btnAllText==”全取消”){$(“#all”).text(“全选”);for(var i=1;i<8;i++){$(“#work”+i).attr(“checked”,”);}}})// 工作日work.click(function(){var btnWorkText = $(“#work”).text();if($(“#work6″).attr(‘checked’)==true || $(“#work7″).attr(‘checked’)==true){$(“#work6″).attr(“checked”,”);$(“#work7″).attr(“checked”,”);return;}if(btnWorkText==”工作日拼车”){$(“#work”).text(“工作日不拼车”);for(var i=1;i<6;i++){$(“#work”+i).attr(“checked”,’true’);/*if($(“#work”+i).attr(‘checked’)==false){$(“#work”+i).attr(“checked”,’true’);}else{$(“#work”+i).attr(“checked”,”);}*/}}else if(btnWorkText==”工作日不拼车”){$(“#work”).text(“工作日拼车”);for(var i=1;i<6;i++){$(“#work”+i).attr(“checked”,”);}}})var n=1;// 点击后增加一个日期输入框,偶发拼车允许多个出发时间。$(“#addDateText”).click(function(){if(n>4){//alert(“只允许添加4个”);pcw_alert(“只允许添加4个”,’0′);return false;}n++;// alert($(“#lineDateM”).attr(“name”));var name = $(“#lineDateM”).attr(“name”);if(name==undefined){name = $(“#lineDate”).attr(“name”);}//生成 0 到10 随机数 Mapth.random产生 0到1的随机数 然后乘以10var maxNumber = 1000;var randomNumber = Math.round(maxNumber * Math.random());var s = “<div id=’div”+randomNumber+”‘><input class=’set_pc_wftime’ type=’text’ name=’”+name+”‘ onclick=\”WdatePicker({dateFmt:’yyyy-MM-dd’, skin:’whyGreen’})\”/>”;s += “ <a href=’javascript:void(0);’ onclick=’del(” + randomNumber + “);return false;’><img src=’images/jian.gif’ /></a></div>”;var $mm = $(s);var $parent = $(“#addText”);$parent.append($mm);})// 点击后增加一个输入框,拼车允许多个标签$(“#addLabel”).click(function(){if(n>4){//alert(“只允许添加4个”);pcw_alert(“只允许添加4个”,’0′);return false;}n++;//生成 0 到10 随机数 Mapth.random产生 0到1的随机数 然后乘以10var maxNumber = 1000;var randomNumber = Math.round(maxNumber * Math.random());var s = “<div id=’div”+randomNumber+”‘><input type=’text’ name=’lineLableDescribe’ maxlength=’12′/>”;s += “ <a href=’javascript:void(0);’ onclick=’del(” + randomNumber + “);return false;’><img src=’images/jian.gif’ /></a></div>”;var $mm = $(s);var $parent = $(“#addTextLabel”);$parent.append($mm);})})// 点击后增加一个日期输入框的删除按钮function del(id) {$(“#div” + id).remove();}// 根据id隐藏divfunction hidenDiv(div) {$(“#” + div).hide();}// 根据id显示divfunction showDiv(div) {$(“#” + div).show();}// 线路发布第一步,修改线路发布时的用户身份function chengeType(id) {if(id==”siji”){$(“#isProvideCar”).val(“1″);}else if(id==”chengke”){$(“#isProvideCar”).val(“2″);}}// 验证汉字,是汉字返回true,否则返回falsefunction isChinese(v){var reg = /^[\u4e00-\u9fa5]+$/i;if (!reg.test(v)){//alert(“请输入中文”);pcw_alert(“请输入中文”,’0′);return false;}else{return true;}}// 验证数字,是数字返回true,否则返回falsefunction isNumber(v){if(isNaN(v)){//alert(“必须是数字”);pcw_alert(“必须是数字”,’0′);return false;}return true;}// 验证手机号码function checkMobile(v){var reg = /^13[0-9]{9}|15[012356789][0-9]{8}|18[0256789][0-9]{8}|147[0-9]{8}$/g;//var reg = /^1((3[4-9])||(5[0-2])||(5[8-9])||(8[8-9])||(82))\d{8}$/g;if(!reg.test(v)) return false;return true;}//判断手机号码是否为空function isMobileNull(evt){//暂时省去校验以环节,.valid()方法为validate.js中的方法,用于验证是否已经通过验证var is_valid = true;if(!$(“#mobilePhone”).val()){is_valid=false;}if(!is_valid){evt.preventDefault();}else{sendAuthenticode(evt.srcElement)}}//验证手机function sendAuthenticode(_this){$.post(“check_sendAuthenticode.html”,{“userInfo.mobilePhone”:$(“#mobilePhone”).val()},function(_result){if(_result==”true”){//alert(“验证码发送成功”);pcw_alert(“验证码发送成功”,’0′);$(“#send_authenticode”).unbind(“click”);// $(_this).attr(“disabled”,”true”);var index=61;var interval_id = setInterval(function(){var sss = “获取验证码” +( –index);//alert($(_this).val());$(_this).text(sss);if(index==0){clearInterval(interval_id);$(_this).text(“获取验证码”);// $(_this).attr(“disabled”,”false”);$(“#send_authenticode”).bind(“click”,isMobileNull);}},1000);}else{//alert(“验证码发送失败”);pcw_alert(“验证码发送失败”,’0′);}});}//在发送邮件前,确认邮箱非空 且 合法—提交前校验function toSendEmail(){var email = $.trim($(“#email”).val());if(email==”" ){//alert(“请输入邮箱号码”);$(“#email”).focus();callBackEmail(“false”);pcw_alert(“请输入邮箱号码”,’0′);return false;}else if(!checkEmail(email)) {//alert(“邮箱号码格式不对!”);callBackEmail(“false”);pcw_alert(“邮箱号码格式不正确!”,’0′);return false;}callBackEmail(“true”);}//发送邮箱验证码function sendEmail(){$.post(“check_sendEmailcode.html”,{“userInfo.email”:$(“#email”).val()},function(_result){if(_result!=”false”){alert(“验证码发送成功,请登录邮箱查看”);$(“#send_emailcode”).unbind(“click”);// $(_this).attr(“disabled”,”true”);var index=61;var interval_id = setInterval(function(){var sss = “获取验证码” +( –index);//alert($(_this).val());$(“#send_emailcode”).text(sss);if(index==0){clearInterval(interval_id);$(“#send_emailcode”).text(“获取验证码”);// $(_this).attr(“disabled”,”false”);$(“#send_emailcode”).bind(“click”,sendEmail);}},1000);}else{alert(“验证码发送失败”);}});}function checkEmail(m){var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;//var reg = /^1((3[4-9])||(5[0-2])||(5[8-9])||(8[8-9])||(82))\d{8}$/g;if(!reg.test(m)) return false;return true;}// javascript 日期格式化Date.prototype.format = function(format) //author: meizz{var o = {“M+” : this.getMonth()+1, //month“d+” : this.getDate(), //day“h+” : this.getHours(), //hour“m+” : this.getMinutes(), //minute“s+” : this.getSeconds(), //second“q+” : Math.floor((this.getMonth()+3)/3), //quarter“S” : this.getMilliseconds() //millisecond}if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(this.getFullYear()+”").substr(4 – RegExp.$1.length));for(var k in o)if(new RegExp(“(“+ k +”)”).test(format))format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] :(“00″+ o[k]).substr((“”+ o[k]).length));return format;}//取得日期时间加几天字符串,返回YYYY-MM-DD HH:mmfunction getDateTime(addDay){var date = new Date();var thisYear = date.getFullYear();var thisMonth = date.getMonth() + 1;//如果月份长度是一位则前面补0if(thisMonth<10) thisMonth = “0″ + thisMonth;var thisDay = date.getDate();//当前天数后几天if(addDay>1) thisDay += addDay;//如果天的长度是一位则前面补0if(thisDay<10) thisDay = “0″ + thisDay;var thisHour = date.getHours();//如果小时长度是一位则前面补0if(thisHour<10) thisHour = “0″ + thisHour;var thisMinute = date.getMinutes();//如果分钟长度是一位则前面补0if(thisMinute<10) thisMinute = “0″ + thisMinute;//var thisSecond = date.getSeconds();//如果分钟长度是一位则前面补0//if(thisSecond<10) thisSecond = “0″ + thisSecond;return thisYear + “-” + thisMonth + “-” + thisDay + ” ” + thisHour + “:” + thisMinute;//return thisYear + “-” + thisMonth + “-” + thisDay + ” ” + thisHour + “:” + thisMinute + “:” + thisSecond;}//根据日期字符串,和加几天字符串,返回YYYY-MM-DD HH:mmfunction getDateForStr(strDate,addDay){var date = new Date(Date.parse(strDate.replace(/-/g,”/”)));var thisYear = date.getFullYear();var thisMonth = date.getMonth() + 1;//如果月份长度是一位则前面补0if(thisMonth<10) thisMonth = “0″ + thisMonth;var thisDay = date.getDate();//当前天数后几天if(addDay>1) thisDay += addDay;//如果天的长度是一位则前面补0if(thisDay<10) thisDay = “0″ + thisDay;var thisHour = date.getHours();//如果小时长度是一位则前面补0if(thisHour<10) thisHour = “0″ + thisHour;var thisMinute = date.getMinutes();//如果分钟长度是一位则前面补0if(thisMinute<10) thisMinute = “0″ + thisMinute;//var thisSecond = date.getSeconds();//如果分钟长度是一位则前面补0//if(thisSecond<10) thisSecond = “0″ + thisSecond;return thisYear + “-” + thisMonth + “-” + thisDay + ” ” + thisHour + “:” + thisMinute;//return thisYear + “-” + thisMonth + “-” + thisDay + ” ” + thisHour + “:” + thisMinute + “:” + thisSecond;}

 

转载自:http://blog.csdn.net/javaalpha/article/details/7850676