我有其中一个用户输入一个十进制数,然后从下拉菜单中,他选择通过点击,如果他想,要么转换为二进制,八进制或十六进制形式转换按钮。新答案应以新形式显示,例如:“二进制数字是......”。但是我所做的代码似乎并不奏效。任何帮助将不胜感激。这是我到目前为止的代码:转换为八进制,二进制和十六进制在JavaScript
<html>
<head>
<title> Convertor </title>
<style>
.panel {
width:400px;
height:160px;
background-color: #E6E6FA;
border:2px solid blue;
font-weight: bold;
font-size: 110%;
margin-left:30px;
margin-top:15px;
}
p {
margin-left: 30px;
margin-top: 15px;
}
form {
margin-left: 30px;
margin-top: 15px;
}
button {
margin-left:40px;
margin-top:10px;
}
.answer {
width:400px;
height:90px;
background-color: #E6E6FA;
border:2px solid blue;
font-weight: bold;
font-size: 110%;
margin-left:30px;
margin-top:15px;
}
form {
margin-left: 70px;
margin-top: 65px;
}
</style>
</head>
<body>
<div class="panel">
<p>
Enter decimal number to convert, select Base and click CONVERT.
</p>
<form>
<input type="text">
<select id="selectid" name="selectname">
<option value="binary">Binary</option>
<option value="octal">Octal</option>
<option value="hexadecimal">Hexadecimal</option>
</select>
<button id="button1" name="Button1" onclick="Answer()"> Convert
</button>
</form>
</div>
<div class="answer">
<form>
</form>
</div>
<script>
function Answer() {
if (document.getElementbyId ('selectid').value=="binary") {
this.value=this.value.toString(2);
}
else if (document.getElementbyId ('selectid').value=="octal") {
this.value=this.value.toString(8);
}
else if (document.getElementbyId ('selectid').value=="hexadecimal") {
this.value=this.value.ToString(16);
}
}
</script>
</body>
你知道'号#toString'的接受基数只是不使用它由于某种原因(学习等)的能力?例如'(15).toString(16)'是''f“'。 '(8).toString(2)'是'“1000”'。 –
而你却用它在你的'toBin',只是采用这种形式,以你的'toOct'与'的toString(8)'等等,那么它的完成。但是,您可能需要确定'this.value'的来源,我没有看到任何设置它的代码。 – fuyushimoya
我在这里闻到作业 –