2010-11-22 5 views
0

私のブログには複数のフォームがあります。フォームごとに、送信ボタンの前にテキストエリアのテキストを変換する変換ボタンを追加しました。したがって、3つのジョブがあります。価値を得る、テキストを計算する、古いテキストを新しいテキストに置き換えます。ボタンのHTMLでは が、私は2つの方法を試していない、どちらも動作します:テキストを計算された新しいテキストに置き換えますonclick = "myfunction()"

myfunction(){ 
oldtext = document.getElementsByTagName('textarea')[0].value; 
...calculating... 
newtext= the result of calculation; 
document.getElementsByTagName('textarea')[0].value = newtext; 
} 

I:

<a onclick="myfunction()" class="button" href="javascript:void(0)">Convert</a>` 

<a onclick="check('document.getElementsByTagName('textarea')[0].value','myfunction()')" 
    class="button" href="javascript:void(0)">Convert</a>` 

をjsの外部ファイルでは、私は、テキストを計算するために、この機能を持っていますボタンまたはmyfunction()に問題があるかどうかは分かりません。助けてください!

+0

「値」はすべてのブラウザで固いです。より多くのコードを見る必要があります。 –

+0

それはokを実行するようです:[jsfiddle.net](http://jsfiddle.net/thecheesewheel/2Ch9e/15/)あなたはそれが期待するものを言うことができますか? –

答えて

0

使用のinnerHTMLの代わりに、値

oldtext = document.getElementsByTagName('textarea')[0].value; 

oldtext = document.getElementsByTagName('textarea')[0].innerHTML; 

に感謝します。

+0

いいえいいえいいえ。 'value'は常に正しいプロパティです。ユーザーがテキストエリアの値を変更すると、 'innerHTML'は機能しません。 –

+0

ありがとう!私はinnerHTMLに変更しましたが、まだ動作していません。他の方法? – Jenny

+0

おそらく、myfunction()の終わりに、 "write"という行を追加するべきでしょうか? – Jenny

1

これはテキストを計算する関数です。関数とボタンの間に何かがないかもしれませんか?

var vowels = new Array ("a","e","i","o","u","v","ü"); 
var umlatu = "ü"; 
var tones = new Array ("a","e","i","o","u","u","á","é","í","ó","ú","u","a", "e", "i", "o", "u", "u","à","è","ì","ò","ù","u"); 


function myfunction() { 

textin = document.getElementsByTagName('textarea')[0].value; 
textin.toLowerCase(); 

currentword = ""; 
currentchar = ""; 
i = 0; 
numletters = textin.length; 
textout = ""; // final output 
tempword = ""; 
usevowel = 1; // which vowel will have the tone over it 
foundvowel = 0; 

for (i=0; i<=numletters; i++) { 
currentchar = textin.charAt (i); 
currentnumvalue = currentchar - 1; 

// numbers 1-5 are tone marks, build the word until we hit one 
if (!(currentchar.match(/[1-5]/))) { 
if (currentchar.match(/[aeiouvü]/)) foundvowel++; 
// if the last character was a vowel and this isn't... 
if (((foundvowel != 0)) && (currentchar.match(/[^aeiouvüngr]/)) || (currentchar == "")) { 
textout = textout + currentword; 
currentword = currentchar; 
} 

else { 
currentword = currentword + currentchar; 
} 
}// if !match 1-5 
// the character must be a tone mark 
else { 

tempword=""; // the word being built in this loop 
foundvowel = 0; // number of vowels found in the word 
usevowel = 1; // which vowel (1st or 2nd) will get the tone mark 

// step through each character in word 
wordlen = currentword.length; 
// If it doesn't have letters, just output it 
if (!(currentword.match(/[a-zü]/))) { 
    textout = textout + currentword + currentchar; 
    currentword = ""; 
} 
// the tone goes over the second vowel for these combinations 
if (currentword.match(/i[aeou]/)) usevowel = 2; 
if (currentword.match(/u[aeio]/)) usevowel = 2; 
if (currentword.match(/[vü]e/)) usevowel = 2; 

// We'll check either the first or the first two vowels, depending on which should have the tone 
for (j=0; (j<=wordlen) && (foundvowel<usevowel); j++) { 
// Check to see if the character is a vowel 
for (vowelnum=0; vowelnum<7; vowelnum++) { 

if (currentword.charAt (j) == vowels [ vowelnum ]) { 
// It's a vowel - convert to corresponding numbered tone character from tones array 
// If tone is 5th (Neutral tone) - Leave it as the normal vowel 

if (currentnumvalue<=3) { 
if (vowelnum == 6) currentchar = tones [5 + (currentnumvalue *6)]; // Handle the damned ü for Europeans who can input it directly 
else currentchar = tones [ vowelnum + (currentnumvalue * 6)]; 
} 

else { 
if (vowelnum == 5) currentchar = umlatu; //neutral tone umlat 
else currentchar = vowels [ vowelnum ]; //all other neutral tones 
} 

foundvowel++; // Increment the counter for vowels found in the word 

if (foundvowel>=usevowel) { 
// rebuild word with the tone if this vowel should have the tone 

tempword=""; 
for (k=0; k<=wordlen; k++) { 
if (k == j) { 
tempword = tempword + currentchar; 
} 

else { //just copy from the input, but turn all remaining v's into umlated u's 
if (currentword.charAt(k) == vowels[5]) tempword = tempword + umlatu; 
else tempword = tempword + currentword.charAt(k); 
} 
} 
currentword=""; 
} 
} 
} 
textout = textout + tempword; 
} // else -deal with numbers 

} 
} 

// rough check 
if (textout=="") alert ('That was not valid input\nCheck http://www.romanization.com to learn about pinyin'); 
else { 
document.getElementsByTagName('textarea')[0].value = textout; 
} 
} 
+0

@Tim Down:助けてくれてありがとう!コードを確認するのを助けてくださいますか? – Jenny