我有一个简单的表格来输入配偶的细节。表格如下;
html
<div class="form-group">
<input type="text" name="name" id="name" placeholder="Name">
</div>
<div class="form-group">
<input type="text" name="ic_no" id="ic_no" placeholder="IC No.">
</div>
<div class="form-group">
<input type="date" name="dob" id="dob" placeholder="DOB" >
</div>ic_no将像870505113223一样。我可以得到第一个6位数,这是我们出生的YYYY-mm-dd;
jQuery
$("#ic_no").blur(function(){
var ic_no = $("#ic_no").val();
var dob = ic_no.substr(0, 6);
console.log(dob); //will output 870505
$("#dob").val(dob); //this will give an error
});我的问题是,我想如何得到6位数字来自动填写道布的输入?
从上面的代码,我会得到这个错误,
The specified value "870505" does not conform to the required format, "yyyy-MM-dd".发布于 2017-06-20 08:31:24
假设870505与日期1987-05-05匹配。
你可以这样做:
var y = "19" + dob.substring(0, 2);
var m = dob.substring(2, 4);
var d = dob.substring(4, 6);
var f = y + "-" + m + "-" + d;
$("#dob").val(f);发布于 2017-06-20 09:02:52
在这种情况下,您不能只使用6位数字进行转换。您必须获得ic_no的所有时间戳,然后将其转换为:
$("#ic_no").blur(function(){
var ic_no = $("#ic_no").val();
d = new Date(ic_no),
month = d.getMonth()+1, //it is begin with 0 so you must add 1
day = d.getDate(),
year = d.getFullYear();
month = CheckZero(month);
day = CheckZero(day);
//check if you can add 0 or not
function CheckZero(number) {
if (number < 10) {
number = "0" + number;
}
return number;
}
var finished_date = year+'-'+month+'-'+day;
$("#dob").val(finished_date );
});https://stackoverflow.com/questions/44647556
复制相似问题