Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript] Dynamiczne tworzenie kolejnych pól formularza, po kliknięciu [+] dodaje się owe pole <input>
Athlan
post
Post #1





Grupa: Developerzy
Postów: 823
Pomógł: 12
Dołączył: 18.12.2005

Ostrzeżenie: (0%)
-----


Cześć, generalnie chodzi mi o to, aby po naciśnięciu linku + dodawało się kolejne pole formularza, np:
  1. <input type="text" name="pole[]"><br />

po naciśnięciu plusa trzykrotnie dynamicznie stworzą się kolejne nowe pola jedno pod drugim:
  1. <input type="text" name="pole[]"><br />
  2. <input type="text" name="pole[]"><br />
  3. <input type="text" name="pole[]"><br />
  4. <input type="text" name="pole[]"><br />


macie namiary na jakieś gotowe skrypty lub (lepiej (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) ) jak napisać takie coś samemu (chodzi o generalną strukturę kodu i funkcje, których powinienem użyć, no i w jaki sposób mają się tworzyć nowe pola bo document.write() nie działa (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) )?

pozdrawiam (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
babejsza
post
Post #2





Grupa: Zarejestrowani
Postów: 407
Pomógł: 1
Dołączył: 4.03.2003
Skąd: warszawa

Ostrzeżenie: (0%)
-----


Pod ręką mam coś takiego, po przeróbkach jak znalazł (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

  1. <script type="text/javascript">
  2.  
  3. i=0;
  4. function wstaw() {
  5. i++;
  6. var tekst = document.createTextNode('pole '+i+': ');
  7. var input = document.createElement('input' );
  8. input.setAttribute('type', 'text');
  9. input.setAttribute('name', 'p[]');
  10. input.setAttribute('size', '4');
  11. var br = document.createElement('br');
  12. var div = document.createElement('div');
  13. div.appendChild(tekst);
  14. div.appendChild(input);
  15. div.appendChild(br);
  16. document.getElementById('pola').appendChild(div);
  17. }
  18.  
  19.  
  20. function wstaw2() {
  21. i++;
  22. var tekst = document.createTextNode('pole '+i+': ');
  23. var input = document.createElement('textarea');
  24. input.setAttribute('name', 'p[]');
  25. input.setAttribute('rows', '3');
  26. input.setAttribute('cols', '100');
  27. var br = document.createElement('br');
  28. var div = document.createElement('div');
  29. div.appendChild(tekst);
  30. div.appendChild(input);
  31. div.appendChild(br);
  32. document.getElementById('pola').appendChild(div);
  33. }



w body:

  1. <form action="index.php" method="post">
  2. <p>
  3. <input type="button" value="input" onclick="wstaw()" />
  4. <input type="button" value="textarea" onclick="wstaw2()" />
  5. </p>
  6.  
  7. <p>
  8. <input type="submit" value="wyślij pola" />
  9. </p>
  10. </form>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 4.10.2025 - 02:48