2016-04-12 10 views
2

私は自分が望む数の著者を追加できる動的フォームを作成しました。関数add_author()insertBeforeがフォームで機能しなくなるdivを追加すると

<form action="uploadtodb.php" target="dummyframe" method="post" name="myform"> 
      <p> 
      Authors last name:  <input type="text" name="authorln[]" size="32" /> 
      Authors first names: <input type="text" name="authorfns[]" size="32" /> 
      </p> 
      <p id="add_author_button"> 
      <button type="button" onclick="add_author()">Add new author</button> 
      <button type="button" onclick="remove_author()">Remove last author</button> 
      </p> 
</form> 

それは完璧に動作しますが、その後、私はそれにいくつかのスタイルを追加したいパラ

document.myform.insertBefore(para, document.getElementById("add_author_button")); 

として以下に示さあるmyForm新しい段落を追加します。だからdivを追加しました:

<form action="uploadtodb.php" target="dummyframe" method="post" name="myform"> 
     <div id="authors"> 
      <p> 
      Authors last name:  <input type="text" name="authorln[]" size="32" /> 
      Authors first names: <input type="text" name="authorfns[]" size="32" /> 
      </p> 
      <p id="add_author_button"> 
      <button type="button" onclick="add_author()">Add new author</button> 
      <button type="button" onclick="remove_author()">Remove last author</button> 
      </p> 
     </div> 
</form> 

このような変更ボタンを押しても反応しません。

+0

クラスまたはIDを追加してフォームのスタイルを変更してみませんか?フォームをコンテナとして扱うことができます – Erick

+0

@Erick私はフォームを別のスタイルに分割することに決めました。質問のために私はちょうど著者を抽出した。 –

+0

'para'はもはや' myform'の子ではないので、 'document.myform.insertBefore'はもはや期待どおりに機能していないかもしれません。 – RJM

答えて

2

フォームにはdocument.<form name>を使用できますが、作成者divのような任意の要素に対しては使用できません。なぜgetElementById()を使用しないのですか?

document.getElementById("authors").insertBefore(para, document.getElementById("add_author_button")); 
+0

それは完全に動作します。私は 'document.getElementById(" authors ")。(...)'が 'myform'にもあるはずであることを忘れるかもしれませんが、私はチェックしてすべてをサーバにアップロードします。 –

関連する問題