键入时显示值,但仅输入前6个数字,并更改数字的顺序

时间:2018-08-01 01:26:53

标签: php jquery html autofill

当我在ID NO字段中键入时,我有2个字段(前六个数字是出生日期,带有YYMMDDxxxxxx(始终包含12个数字),DOB字段将根据ID NO自动填充,但是DOB的值仅在ID NO字段中包含前6个数字,顺序为DDMMYY

示例:

  

编号: 931121 091010(YYMMDD)

     

DOB: 211193 (DDMMYY)

var $dob = $("#dob");
   $("#id_no").keyup(function() {
   $dob.val(this.value);
   });
  $("#id_no").blur(function() {
    $dob.val(this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<form>
  <div>
  <label>ID NO</label>
    <input type="text" name="id_no" id="id_no" class="form-control" onKeyPress="return 
       goodchars(event,'1234567890',this)" required="" value="">
  </div>
  <div>
  <label>DOB</label>
    <input type="text" name="dob" id="dob" class="form-control" value="">
  </div>
</form>

1 个答案:

答案 0 :(得分:0)

请参见下面的实时示例:

var $dob = $("#dob");
$("#id_no").keyup(function() {
  var str = this.value;
  var strYYMMDD = str.substring(0, 6);
  if (str.length > 6) {
    var strDDMMYY = strYYMMDD.substring(4, 6) + strYYMMDD.substring(2, 4) + strYYMMDD.substring(0, 2);
    $dob.val(strDDMMYY);
  } else {
    $dob.val("");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <div>
    <label>ID NO</label>
    <input type="text" name="id_no" id="id_no" class="form-control" maxlength="12" onKeyPress="return 
       goodchars(event,'1234567890',this)" required="" value="">
  </div>
  <div>
    <label>DOB</label>
    <input type="text" name="dob" id="dob" class="form-control" value="">
  </div>
</form>