input type="date" 赋值
现在的html5 input标签支持type=“date” 显示有日期的日历控件,一行简单的代码就能显示出一个日历控件,但是有的时候需要给它一个默认的日期值,这个时候可能就要用到val(), attr(“value”,"")等方法,有可能很多人会不成功,其实并不是这个标签不支持这些赋值的方法,而是赋值的时候字符串的格式有问题。
解决办法:一说就明白,一般我们用new Date().getMonth() new Date().getDate()方法获得日期的时候如果日期与月数小于10,则默认显示为1,2,3,4,5,6,7,8,9 所以日期的格式可能就是2013-9-5。而Input type="date"标签中支持显示的日期格式为2013-09-05,把这个值传给input标签就能够正常显示日期的值了,所以这里要加一个判断,如果月数或者日期数是小于10的,就要在其前面添加数字0构成字符串,这样就能赋值后正常显示了。
var now = new Date();
alert(now.getDate()+’-’+now.getMonth());
//格式化日,如果小于9,前面补0
var day = (“0” + now.getDate()).slice(-2);
//格式化月,如果小于9,前面补0
var month = (“0” + (now.getMonth() + 1)).slice(-2);
//拼装完整日期格式
var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
//完成赋值
$(’#createStartTime’).val(today);
作者:yusirxiaer
来源:CSDN
原文:https://www.geek-share.com/detail/2706524207.html
版权声明:本文为博主原创文章,转载请附上博文链接!
- 点赞
- 收藏
- 分享
- 文章举报
- HTML中INPUT type="date"标签如何赋值注意问题
- HTML中INPUT type="date"标签如何赋值注意问题
- 时间戳赋值给input type=date的标签上
- html中input标签type=date 的赋值问题
- input type date 解决移动端显示placeholder.
- js有关input type=date传值类型
- 动态input type="radio"选中,而不是赋值
- 每日一剂:怎么间接调用input type="file"和type="date"
- 添加账单(通过ajax加载供应商对应商品)Input边框、DatePicker、returnData-i加入商品td项赋值
- HTML5中input[type='date']自定义样式&日历校验功能
- 用JS替 <input type=file>的value属性动态赋值
- fastclick插件 导致 input[type="date"] 无法触发问题解决方案
- 关于如何给<input type="date">设置默认当前日期的方法 和 给table固定宽度
- input[type="date"]默认样式修改
- input[type="date"]默认样式修改
- html中input type="text" 的readonly 属性与asp.net服务器控件TextBox的readonly属性的js赋值、调用的小bug
- <input type="date">设置默认当前日期
- H5中input[type="date"]默认样式修改 伪类
- 更改input type=“date”等元素的默认样式
- type=‘date’的input标签