[JS] input 取的值不管是啥都是字串(string)
<input type="number" id="myinput">
//因為input取的值是字串(string) 所以取值要轉成數字(number)
//所以即使input 的type設定為number 但typeof去檢測都是string
所有原文
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="number" id="myinput">
<button onclick="func()">go</button>
<br>
<div id="output"></div>
<script>
let myinput = document.querySelector('#myinput');
let output = document.querySelector('#output');
function func(){
let i = parseInt(myinput.value);
//因為input取的值是字串(string) 所以取值要轉成數字(number)
//所以即使input 的type設定為number
console.log(i)
// let i = 255;
// let j = i.tostring(i,16);
let j = i.toString(16);
// console.log(i.toString(16));
output.innerHTML = j;
}
</script>
</body>
</html>
留言
張貼留言