2014-03-28 35 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