2012-04-07 3 views
0

아스키로 변환 텍스트 만들려고 :간단한 나는 HTML 양식이

<html> 
<body> 
<form> 
<textarea></textarea> 
<input type="button" value="Submit" onclick="????"></input> 
</form> 
<script type="text/javascript"> 
var code, chr = 'here'; 
code = chr.charCodeAt(0); 
document.write("The ASCII code of " + chr +" is "+code); 
</script> 
</body> 
</html> 

내가 어떤 사용자에/내가 그의 텍스트 (단일 문자)를 넣어하는 텍스트 영역을하려고 해요을하고를 이 버튼을 클릭하면이 이벤트가 트리거됩니다.

var code, chr = '`here`'; 

필요한 추가 데이터가 있으면 알려 주시기 바랍니다 :

이제 문제는 내가 제출 버튼을가 텍스트 영역에 무엇이든 제출해야합니까 방법이다.

+1

1)'document.write'를 사용하지 마십시오. 2) 모든 문자를 반복하여 문자 코드를 가져옵니다. 3) 함수에 코드를 포함하고 함수 이름을'onclick' 속성에 추가하십시오. –

+0

두 번째 사항을 이해하지 못했습니다. –

+0

현재 코드는'.charCodeAt (0)'에 첫 번째 문자 (= 인덱스 **'0' **)의 문자 코드 만 표시합니다. 루프를 사용하면 전체 입력 텍스트의 문자 코드를 볼 수 있습니다. –

답변

1

난 당신이 아스키가 입력 변환하는 기능을 필요로 당신이 forL을 찾고 이해하고 당신으로부터 그 전화를해야하는 경우 버튼.

버튼 호출이 모두 상대적인 것이므로 페이지에 15 개의 양식이있을 수 있으며 모든 작업이 가능하며 실제 번역 방법은 간단하고 이산 적으로 유지됩니다.

너무 단순하므로 인라인으로 삭제할 수 있습니다.

<html> 
<body> 
<script type="text/javascript"> 
function asciify(txt) { 
    return = txt.charCodeAt(0); 
} 
</script> 
<form> 
<textarea name="txt_input"></textarea> 
<input type="button" value="Submit" onclick="this.form.txt_output.value = asciify(this.form.txt_input.value)"></input> 
<textarea name="txt_output"></textarea> 
</form> 
</body> 
</html> 
1
<html> 
<body> 
<form> 
<textarea id="text"></textarea> 
<input type="button" value="Submit" id="subm"/> 
<div id="out"></div> 
</form> 
<script type="text/javascript"> 
document.getElementById("subm").addEventListener("click", asciify, false); 
function asciify(){ 
    var ih = document.getElementById("text").innerHTML; 
    var out = []; 
    for(var i = 0; i < ih.length; i++){ 
     out.push(ih.charCodeAt(i)); 
    } 
    document.getElementById("out").innerHTML = out.join(",") 
} 
</script> 
</body> 
</html> 

이것은 온 클릭 처리기를 추가하려면 자바 스크립트를 사용하고, 지원하는 둘 이상의 문자

3

는 OSX 사파리에 만든이, 시도 :

<html> 
<body> 

<form> 
ASCII:<br> 
<textarea id="text" rows="8" cols="50"></textarea><br> 
<input type="button" value="Convert" onclick="asciify()"> 
<br> 
<hr> 
DEC:<br> 
<textarea id="output" rows="8" cols="50"></textarea> 
</form> 

<script type="text/javascript"> 
function asciify(){ 
     var ih = document.getElementById("text").value; 
     var out = ""; 
     for(var i = 0; i < ih.length; i++){ 
     out=out.concat("<",ih.charAt(i).charCodeAt(0),">"); 
     } 
    document.getElementById('output').innerHTML = out; 
} 
</script> 


</body> 
</html> 
관련 문제