2014-03-28 34 views
0

我對JavaScript並不是很熟悉,而且我對它的面向對象的特性有點困惑。我正在嘗試使用JavaScript中的對象,方法和屬性的概念創建一個凱撒班。我正在使用HTML表單從用戶接收輸入,並且OnClick將編碼的密文文本返回給用戶。這是我的。我非常確定自己的邏輯,但我想我的對象創建和方法調用都是通過的。我做對了嗎?任何幫助表示讚賞。 謝謝!創建對象並從HTML中調用JavaScript方法

<head> 
    <script> 
    function Caesar(order){ 
    this.order = order; 
    this.encode = encode; 
    function encode(input){ 
    this.output = ''; 
    for (var i = 0; i < input.length; i++) { 
    var this.c = input.charCodeAt(i); 
    if  (this.c >= 65 && this.c <= 90) this.output += String.fromCharCode((this.c - 65 + this.order) % 26 + 65); // Uppercase 
    else if (this.c >= 97 && this.c <= 122) this.output += String.fromCharCode((this.c - 97 + this.key) % 26 + 97); // Lowercase 
    else this.output += input.charAt(i);  
    } 
    return answer.innerHTML= output; 
    } 
    </script></head> 
    <body> 

    <form> 
    Enter Plaintext : <input type = "text" name = "plaintext"> 
    Enter Shift:  <input type = "text" name = "shift"><br> --How do I get the input from here and create my caesar object? 
    <input type="button" value="Encode" onclick ="encode()"> --How do I call the encode() method with input from the plaintext text box? 
    </form> 
    </body></html> 
+0

是你有什麼問題?如果你所需要的是有人審查你的代碼,請看http://codereview.stackexchange.com/ –

+0

哦,哇。沒有意識到這一點。我想知道我是否以正確的方式進行。將在那裏看看。謝謝。 – an91

回答

0

給明文文本框一個id,通過它可以在JavaScript中接收它的值。

<input type="text" name="plaintext" id="plain_text">

在JavaScript中,通過使用得到的HTML元素:

var plainTextBox = document.getElementById('plain_text');

檢索從文本框中的值是一樣容易:

plainTextBox.value