我有一个输入字段,需要输入年份值,返回2位数字而不是4位数字
示例:从:2019年2月2日起
需要输出,如:02/19
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input id="nmipay-card-expiry" class="input-text" type="text" maxlength="7" autocomplete="off" placeholder="MM / YY" name="nmipay-card-expiry" />
Fiddle here:输入2位数后,在/
之后自动添加space
和/
答案 0 :(得分:0)
如果您输入了 substr ,则可以使用后2位数字
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function GetLast2Digit(){
var year= $("#txtyear").val();
var LastTwoDigitOfyear = ('' + year).substr(5);
console.log(LastTwoDigitOfyear);
alert("Last 2 digits are "+LastTwoDigitOfyear)
}
</script>
</head>
<body>
Enter year
<input type="text" id="txtyear" placeholder="MM / YY"/>
<button type="button" onclick="GetLast2Digit()">Click</button>
<lable id="lblyear" />
</body>
</html>