[英]Conversion from Decimal to octal, binary and hexadecimal in javascript
[英]Javascript Hexadecimal, Binary, Decimal & Octal Converter
<div class="featurebox">
<h1>Javascript Learning</h1>
<div class="bag">
<p>Input Your Value here :
<input id="numb" placeholder="provide your value here">
<button class="btn" onclick="myfunction()">Result</button>
</p>
<table border="1px solid black">
<tr>
<td>Decimal Value</td>
<td>Hexadecimal Value</td>
<td>Octal Value</td>
<td>Binary Value</td>
</tr>
<tr>
<td><p id="text1"></p></td>
<td><p id="text2"></p></td>
<td><p id="text3"></p></td>
<td><p id="text4"></p></td>
</tr>
</table>
</div>
<footer>© Copyright Reserved by Shah Imran </footer>
</div>
<script>
function myfunction(){
var val,text1,text2,text3,text4;
val = document.getElementById("numb").value;
document.getElementById("text1").innerHTML= val.toString(10);
document.getElementById("text1").innerHTML= val.toString(16);
document.getElementById("text1").innerHTML= val.toString(8);
document.getElementById("text1").innerHTML= val.toString(2);
}
</script>
代码有问题吗? 我这里没有附上 css 代码。 但是 output 在每个字段中显示相同的值。 为什么会这样? 有没有与变量有关的问题?
您首先需要将字符串值解析为 integer:
val = document.getElementById("numb").value;
应该
val = parseInt(document.getElementById("numb").value);
function myfunction() { var val, text1, text2, text3, text4; val = parseInt(document.getElementById("numb").value); document.getElementById("text1").innerHTML = val.toString(10); document.getElementById("text2").innerHTML = val.toString(16); document.getElementById("text3").innerHTML = val.toString(8); document.getElementById("text4").innerHTML = val.toString(2); }
<div class="featurebox"> <h1>Javascript Learning</h1> <div class="bag"> <p>Input Your Value here:<input id="numb" placeholder="provide your value here"> <button class="btn" onclick="myfunction()">Result</button> </p> <table border="1px solid black"> <tr> <td>Decimal Value</td> <td>Hexadecimal Value</td> <td>Octal Value</td> <td>Binary Value</td> </tr> <tr> <td> <p id="text1"></p> </td> <td> <p id="text2"></p> </td> <td> <p id="text3"></p> </td> <td> <p id="text4"></p> </td> </tr> </table> </div> <footer>© Copyright Reserved by Shah Imran </footer> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.