js截取字符串计算
程序员文章站
2022-06-01 15:14:23
...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
window.onload = function(){
var pt = document.getElementById('pt');
var btn = document.getElementById('btn');
btn.onclick = function(){
var value = pt.value;
// console.log(value);
// pt.substr(0,1);
console.log(value.substr(0,1));
console.log(value.substr(1,1));
console.log(value.substr(2,1));
alert(Number(value.substr(0,1)) + Number(value.substr(1,1)) + Number(value.substr(2,1)));
}
}
</script>
</head>
<body>
请输入一个3位数:
<input type="text" name="" id="pt" value="" />
<button type="button" id="btn">计算</button>
</body>
</html>