2015-06-20 165 views
1

我有其中一个用户输入一个十进制数,然后从下拉菜单中,他选择通过点击,如果他想,要么转换为二进制,八进制或十六进制形式转换按钮。新答案应以新形式显示,例如:“二进制数字是......”。但是我所做的代码似乎并不奏效。任何帮助将不胜感激。这是我到目前为止的代码:转换为八进制,二进制和十六进制在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> 

+3

你知道'号#toString'的接受基数只是不使用它由于某种原因(学习等)的能力?例如'(15).toString(16)'是''f“'。 '(8).toString(2)'是'“1000”'。 –

+0

而你却用它在你的'toBin',只是采用这种形式,以你的'toOct'与'的toString(8)'等等,那么它的完成。但是,您可能需要确定'this.value'的来源,我没有看到任何设置它的代码。 – fuyushimoya

+0

我在这里闻到作业 –

回答

0

如果你想将数字转换为十六进制表示,你可以使用toString方法。 toString的第一个参数可以是数字的基数。 例子:

var n = 12; 
n.toString(); // "c" 

如果你想转换回,你可以使用parseInt函数...

var hexnum = "c"; 
parseInt(hexnum,16); // 12 

这些功能适用于任何基数。

下面是完整的源代码:

<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> 
     <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">Convert</button> 
     </div> 
     <div class="answer" id="answer"> 
     </div> 
     <script> 
     var Answer = function(e) { 
      var radix; 
      var radixStr = document.getElementById('selectid').value; 
      var val = parseInt(document.getElementsByTagName("input")[0].value); 
      switch(radixStr) { 
       case "binary": 
       radix = 2; 
       break; 
       case "octal": 
       radix = 8; 
       break; 
       case "hexadecimal": 
       radix = 16; 
       break; 
      } 

      document.getElementById("answer").innerHTML = val.toString(radix); 
      e.preventDefault(); 
      return false; 
      } 
     document.getElementById("button1").addEventListener("click",Answer); 
     </script> 
     </body> 
    </html> 
+0

感谢您的帮助。然而,Aptana似乎并未奏效。在另一个像w3school这样的编辑器上工作。我不知道为什么 –

+0

在浏览器中试用 –

3

十进制转换为hex/oct/bin

var hex=100.toString(16); // "64" 
var oct=100.toString(8); // "144" 
var bin=100.toString(2); // "1100100" 

和相同的向后:

var dec=parseInt("64",16);  // 100 
var dec=parseInt("144",8);  // 100 
var dec=parseInt("1100100",2); // 100 
1

你可以直接转换成你的价值观二进制和八进制和十六进制柯本

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); 
    } 
} 
+0

为什么你保留'toHex'呢? – fuyushimoya

+0

,因为JavaScript没有任何内置的方法来转换为十六进制 –

+0

我的不好,我们实际上可以转换为十六进制。我编辑了我的答案 –

0

我看到一些错误

它不是的getElementById ==>是的getElementById, 不好用的情况下(这)的...在你的答案的功能(这)是指向到按钮..我想你需要改变输入值,右

试试这个:

function Answer(e) { 
    e.preventDefault(); 
    var input = document.getElementById('input'); 
    if (document.getElementById('selectid').value ==="binary") { 
     input.value = Number(input.value).toString(2); 
    } 
    else if (document.getElementById('selectid').value ==="octal") { 
     input.value = Number(input.value).toString(8); 
    } 
    else if (document.getElementById('selectid').value ==="hexadecimal") { 
     input.value = Number(input.value).toString(16); 
    } 
} 

注:添加ID =“输入”到输入上的HTML部分

3

下面是一些伟大的代码,我只是做二进制,八进制,十进制和十六进制之间的转换:

function id(id) { 
 
    return document.getElementById(id); 
 
} 
 
function Convert(s, n) { 
 
    if(parseInt(id(s).value, n)) { 
 
    if("bin" != s) { id("bin").value = parseInt(id(s).value, n).toString(2) } 
 
    if("oct" != s) { id("oct").value = parseInt(id(s).value, n).toString(8) } 
 
    if("dec" != s) { id("dec").value = parseInt(id(s).value, n).toString(10) } 
 
    if("hex" != s) { id("hex").value = parseInt(id(s).value, n).toString(16) } 
 
    } else { 
 
    if("bin" != s) { id("bin").value = "" } 
 
    if("oct" != s) { id("oct").value = "" } 
 
    if("dec" != s) { id("dec").value = "" } 
 
    if("hex" != s) { id("hex").value = "" } 
 
    } 
 
}
<input id="bin" oninput="Convert('bin', 2)" placeholder="bin" spellcheck="false"> 
 
<input id="oct" oninput="Convert('oct', 8)" placeholder="oct" spellcheck="false"> 
 
<input id="dec" oninput="Convert('dec', 10)" placeholder="dec" spellcheck="false"> 
 
<input id="hex" oninput="Convert('hex', 16)" placeholder="hex" spellcheck="false">

小而简单,完美的人试图找到一种。

相关问题