0

html コード:

<textarea id="TaAll" rows=11>
 </textarea>

<input type="button" Value="Get Results" 
 onclick="doit();"/>

<textarea id="Tanext" rows=11>
  </textarea>

JavaScript コード:

window.doit = function ()
{
console.log(document.getElementById("TAall").value);
var containertext=document.getElementById("TAall").value.split("\n");  
    for(var i=0;i< containertext.length;i++)
      {
       var temp=containertext[i+1].split(" "); 
       sbray[i]=eval(temp[1].substring(1,temp[1].length)); 
       op[i]=eval(temp[2].substring(1,temp[2].length)); 


       } 
 Tb = document.frmone.Tanext;


 Tb.value = Tb.value +("\n")+sbray.join("\n")+ ("\n")+"Test";




keysbyValue(); 
}

こんにちは、上記のコードには 2 つのテキスト領域があります。入力を TAall textarea に書き込んで分割し、その部分文字列を他の配列 sbray[] と op[] に保存しています。テキストエリアに containertext[] を表示するまでのコードは正常に動作しますが、結果の sbray[] を他のテキストエリア Tbnext に表示しようとすると動作しません。私が使用している入力は次のとおりであり、この形式の入力を使用する必要があります。私は配列を分割し、すべての番号の部分文字列を保存したい. sbray[] に左の「c」、op[] に右の「c」を追加:

10
1 c1 c2 //i want to save and split the array starting from this line.
2 c3 c4
3 c5 c12
4 c6 c7
5 c8 c11
6 c9 c10
7 c13 c15
8 c14 c16
9 c17 c18
10 c19 c20

前もって感謝します

4

3 に答える 3

0

に渡しTAallましdocument.getElementByIdたが、idは ですTaAll。要求したオブジェクトが存在しないため、エラーが発生します。

0また、 からまで繰り返してcontainertext.length-1いますが、 を求めていますcontainertext[i+1]。配列の最大のインデックスはarray.length-1(0 ベースのインデックス) であるため、containertext[containertext.length]エラーが発生します。

于 2012-04-19T19:41:21.613 に答える
0

document.getElementById大文字と小文字が区別されるので、 に変更TAallするとTaAll、よりうまく機能するはずです。

他にもいくつかの間違いがあります。たとえば、temp へのスコープが失われるため、eval を使用しないでください。

スクリプトを機能させようとしましたが、ここでその動作を確認できます: http://jsfiddle.net/KjYpX/

編集:

私はついにあなたが望んでいたことを理解しました。ここでスクリプトを試すことができます : http://jsfiddle.net/KjYpX/1/

奇妙なフォーマットを解析する関数の詳細は次のとおりです。

var parseMyFormat = function(input){ // we declare a new function
  var sbray=[], op=[]; // declaration of two temporary array
  (input=input.split('\n')).shift(); // we split input in an array called input containing all lines, and then we shift it, it means we remove the first element.

  while(o=input.shift()){ // now, we are gonna make a loop, shift remove the first element but also return it, so we save it in the o variable
    sbray.push((o = o.match(/c(\d+) c(\d+)$/))[1]); //we apply a regexp on o to get the two decimal (\d) ids, it returns an array containing for example ['c1 c2','c1','c2'], we save this array in o (because we will not use o anymore), and we push 'c1' in sbray
    op.push(o[2]); // we push 'c2' in op
  }

  return [sbray, op]; // finally we return both arrays so that you can access them easily
}
于 2012-04-19T19:27:59.097 に答える
0

document.getElementById は大文字と小文字を区別します。あなた<textarea>の の ID は ですTaAllが、あなたはそれを取得しようとしていますdocument.getElementById("TAall")

于 2012-04-19T19:27:39.283 に答える